0

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

When using cd in crontab it feels un-natural or hardcoded that's why i'm trying to find a better way.
In my opinion it's more natural. But it's pure opinionated. :-)
Oh, I've forgotten to include 2 more variants: read the directory from an environment variable or from a config file. I'm sure you could invent such methods yourself. And I still think that for a simple case cd in the shell/cron is the simplest and cleanest way. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.