Jump to content

What's the best way to schedule this Python to run on a regular schedule?


Recommended Posts

I'm new to Unraid and recently upgraded my server from Windows 10 and have been successful in replicating almost everything (Plex, Radarr, Sonarr, etc.) except this Python code I had scheduled to run (via a Windows batch script) every day.

 

It simply looks for files in a folder with a certain word and replaces it with another.

 

Is there a better way to do this in Unraid (maybe without Python), or what is the easiest way to schedule Python code to be run?

 

Also, would I have to change anything in the code to get it to work? Right now, I have a share called "movies" that I would want it to search through and I'm not sure what that file path would look like in the Python code (I'm not very familiar with Python either).

 

from glob import glob
import os

files = glob("X:/Media/Movies/**/*")

for f in files:
    os.rename(f, f.replace("-Radarr", "-TigoleQxR"))

 

Link to comment

NerdPack plugin to install Python:

https://forums.unraid.net/topic/35866-unraid-6-nerdpack-cli-tools-iftop-iotop-screen-kbd-etc/

 

User Scripts plugin as mentioned to schedule script:

https://forums.unraid.net/topic/48286-plugin-ca-user-scripts/

 

The user shares are at /mnt/user, so a Movies share is at /mnt/user/Movies. Note that Linux is case-sensitive.

 

Python is not one of my native tongues so I will leave that as an exercise for the reader.;)

Link to comment
10 minutes ago, trurl said:

NerdPack plugin to install Python:

https://forums.unraid.net/topic/35866-unraid-6-nerdpack-cli-tools-iftop-iotop-screen-kbd-etc/

 

User Scripts plugin as mentioned to schedule script:

https://forums.unraid.net/topic/48286-plugin-ca-user-scripts/

 

The user shares are at /mnt/user, so a Movies share is at /mnt/user/Movies. Note that Linux is case-sensitive.

 

Python is not one of my native tongues so I will leave that as an exercise for the reader.;)

Is my Python script saved within the script file that User Scripts will run, or do I need to write a script that will invoke my Python script?

Link to comment
12 minutes ago, Squid said:

either or.  If there's problems, then have a bash script call the python one.

 

Okay. I installed Python via the NerdPack, and then created and ran the following script via User Scripts:

 

from glob import glob
import os

files = glob("mnt/user/test_share/**/*")

for f in files:
    os.rename(f, f.replace("-BeepBopBoop", "-Test"))

And got the following result:

 

image.thumb.png.17a227a367e8ea671ffd2f3b51113e09.png

 

The script ended up not changing the expected file name. Any thoughts on what might be happening? "From" and "import" are Python commands, so I'm not sure how to get User Scripts to recognize that it's running a Python script.

 

If I save the actual Python code as pythoncode.py, how would I invoke that via the script? That may be the easier way to do it.

Edited by HALPtech
Link to comment
4 minutes ago, Squid said:

Is the first line still #!/bin/bash?  Change it to #!/usr/bin/python

Here's what I get when changing the first line to #!/usr/bin/python:

 

image.thumb.png.2e90e51fef1436fdb24a19aa540460c0.png

 

It would seem installing python didn't install a directory it was supposed to have?

 

(I've confirmed it is downloaded, installed, and turned on in the NerdPack settings.)

Edited by HALPtech
Link to comment
1 minute ago, Squid said:

unless you installed python3 in which case you need to use #!/usr/bin/python3

That worked! I had installed python3. 

 

One last question - do I need to run some sort of exit command in my script, or will it automatically stop running after it looks through all the folders in the share I specified?

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...