Sandu Andrei Luis
Sandu Andrei Luis

Reputation: 28

Running python script from crontab?

I need some help running a python script from crontab:

The script looks for subfolders from current path and does something to them, also extracts a zip file located in the same folder of the script into each found subfolder.

When I go with cd /folder/folder then python script.py is all good. But when run it with crontab it runs in users home folder and not where the script is placed.

To overcome this I placed in crontab something like this:

* * * * cd /folder_of_scrpit/ && /python_path/python script.py >> log.txt

and works as needed but feels weird, is there a better way of achieving this?

Upvotes: 0

Views: 573

Answers (1)

phd
phd

Reputation: 94397

You can cd in crontab the way you do it. Or you can call os.chdir() in your script. In the latter case you can write the directory in the script or pass it as a command line argument: /python path/python script.py /folder/folder.

Upvotes: 2

Related Questions