89DEVs

Run Python scripts as Cron job

Sometimes it is required to run Python scripts automatically. Linux and Mac have cron jobs that allow us to do exactly that. We can define to run the script at a specific time or for example every minute of the day. In this tutorial, you will learn everything about setting up cron jobs and some common pitfalls.

List all cronjobs

To list all cron jobs in your system run the following command: crontab -l A list of all currently installed cron jobs is returned. The first part shows when the cron job is executed and the second part shows which kind of script is executed. * * * * * python3 /python/myscript.py In this example, the script myscript.py in the python folder is executed by the python3 interpreter every minute.

schedule cron job

To create a new cron job run the following command which allows us to add commands to the list of existing cron jobs. crontab -e Then simply add the command you wish to execute and close the program by using ^X.

syntax

The syntax of a cron job is quite easy to understand. The first position defines the minute it is run. The second position defines the hour it is run. The third position defines the day of the month it is run. The fourth position defines the month it is run. The fifth position defines the day of the week it is run. It is possible to run cron jobs for example only Sundays when traffic is lower or only on the first of each month. # m h dom mon dow command * * * * * python3 /python/myscript.py The code will run the script myscript.py in the python folder every minute.

pitfalls for Python scripts as cron job

It is important to specify the full path of the python script. Otherwise, the cron job will assume the home directory and might not run correctly. And make also sure that the full path is given for any includes or packages within the executed script. This is a common pitfall that is quite hard to detect. It's not easy to debug a script run by a cron job because there is no output that gives us information about the error. But we can write the output to a file and then view it in VIM or any other text editor. To write the output and errors to a file use the following syntax: * * * * * python3 /python/myscript.py > /python/error.log 2>&1

                
        

Summary


Click to jump to section