cron queueing bash and python scripts
I'm currently looking to understand and get comfortable with crontab queues on my macbook pro. I recently solved an issue where my simple bash script would not execute. It turns out I had to move my bash script and its output in the /Users/myusername/
path. Before I had it in the /Users/myusername/Desktop/
. This post can be found here - https://stackoverflow.com/questions/64110897/simple-bash-script-doesnt-cron-properly?noredirect=1#comment113370465_64110897
My question: Do all of my bash and python scripts need to be housed in /Users/myusername
path always? Is there no workaround?
Solution 1:
Question:
My question: Do all of my bash and python scripts need to be housed in
/Users/myusername
path always? Is there no workaround?
Answer:
No. You can locate your bash and python scripts anywhere you like. However, there are additional requirements:
- Your
cron
jobs run with a different ENVIRONMENT than when you run them; e.g.$PATH
. Consequently, you should use a full path specification for specifying your scripts in thecrontab
; for example use:
* * * * * /opt/local/bin/python3 /some/dir/you/choose/myscript.py
- On my Mac (Catalina) I've found it necessary to give "Full Disk Access" privileges to
cron
. YMMV on a different OS version. Here's how:
System Preferences
-> Security & Privacy
-> Privacy
tab:
- At least while debugging your
cron
job, you should consider redirecting any error messages (stderr
) to a file - otherwise they wind up in/dev/null
which is not helpful! Continuing with the above example:
* * * * * /opt/local/bin/python3 /some/dir/you/choose/myscript.py >> /Home/<username>/MyCronLog.txt 2>&1
Solution 2:
You may just need to be sure you grant permissions to the elements and use full paths for everything. Just because a script runs when you are in an interactive shell, doesn’t mean they are correct in a limited shell.
Here are super debugging tips on a recent question.
- cron and "command not found"
- cronjobs do not run
- Getting unknown command error when trying to run script via cron