Want to have a Python script run every hour on your Ubuntu server? It's easy to setup a Cron job to do this.
It's convenient to add a shebang to your Python script, so you can use it as a shell script:
Run this command:
You may be asked to choose an editor. If so, please choose the editor of your choice (I choose vi. An editor will open:
At the bottom of the file, we can add a line to run a Python script every hour:
The line we added above was this:
* */1 * * * cd /my/python/script/dir && /my/python/script/dir/myscript.py
As shown in this post, you'll probably want to change directories to where you typically run your script. Otherwise your Python script might not find needed modules.
Move the */1 into the minutes spot:
*/1 * * * * cd /my/python/script/dir && /my/python/script/dir/myscript.py
Make sure the exact command can be run manually without errors.
Set the task to run every minute.
As shown here, try piping your command output to a file:
*/1 * * * * cd /my/python/script/dir && /my/python/script/dir/myscript.py >> /my/python/script/dir/results.txt 2&>1
As recommended here, run the following command to view errors:
grep CRON /var/log/syslog
Leave a comment