the Power to change anything.

How to automate Python scripts with Cron

Think about all the scripts that you ran manually for more than one time. There is quite a bit, right. Probably that’s repetitive work. For all the good reasons why process automation is a good idea, we need to change this.

In this article, we will work with the Linux-born scheduling tool Cron to schedule Python scripts. All steps have been executed on a Ubuntu 20.04 distribution.

Help to improve this post on GitHub. Thank you for your contribution.

Installing Cron

Let’s get Cron installed on our Ubuntu machine so that we get ready for scheduling jobs. We will do this with three simple sudo commands. First, we check the system for updates to make sure that we get the latest version.

sudo apt update

Then, we run the installation process for Cron.

sudo apt install cron

Lastly, we allow Cron to run in the background of our machine.

sudo systemctl enable cron

Congrats! You have just successfully installed Cron on your Ubuntu machine.

The crontab

As introduced above, we will realize the Cron automation through schedulung jobs. Cron jobs live in a file format named crontab. Each user of your system can own their own crontab file to record and manage their job schedule (file path: /var/spool/cron/crontabs/).

For editing your crontab, open the file with the following command.

crontab -e

If you are opening crontab for the first time, you’re asked to make a choice for which text editor to use for crontab. Make a selection and allow the file to open.

In case you have set up a bunch of jobs in your crontab, you can read its contents with this quick command. It’s particularly handy, as you don’t need to open the text editor and don’t risk any unintended changes.

crontab -l

Remove your crontab with the following command. Be alerted that there is no further pop-up window about whether you want to confirm this action. Once commanded, the crontab is removed.

crontab -r

Do you want to know how we change everything?

Schedule a call and we can show you how to boost your business.

For creating or changing a cron job, we open the file crontab and write our commands in a language called cron expression. The standard layout is divided into 5 fields for the timing and concludes with the command that you want to execute. For illustrative purpose:

minute hour day_of_month month day_of_week command_to_run

To explore and test whether your command is giving you the expected timing, check this: https://crontab.guru. Very much recommended!

If you’re not particular about the specific minute or hour for your repititions, you can also make use of shortcut codes. These follow a different syntax starting with a @ symbol, like @weekly. Here is an overview for the shortcuts and how each translates into the standard cron expression syntax or plain English.

 

Shortcut Shorthand for Translated into plain English
@hourly 0**** “At minute 0.”
@daily 00*** “At 00:00.”
@weekly 00**0 “At 00:00 on Sunday.”
@monthly 001** “At 00:00 on day-of-month 1.”
@ yearly 0011* “At 00:00 on day-of-month 1 in January.”

Set up Cron job for a Python script

Now comes the part all of us Pyhton-script-holders have been waiting for: How to command a Python script execution. Here is an example that we can use for further discussion.

@daily /usr/bin/python3 /home/siegstedt/projects/machinemind/job.py

The cron expression syntax still applies as described above. In the example, the shortcut @daily was used instead of 00***.

Further, for the case that you want to run a script from file, the command consists of at least two parts. First, you need define which python version executable to use. You can easily find that out by hitting the following keys in your terminal. Please put special attention to this step if you’re working with a project-specific virtual environment.

which python

And secondly, you need to define the path to your python script. As shown in the example above, use the full path.

Yay! Brilliant work. Your jobs are up and running.

 

To conclude our work on the automation, we set up a logging file for controlling our tool. Now, look how simple it is to realize that. Let’s pick up our example from above.

@daily /usr/bin/python3 /home/siegstedt/projects/machinemind/job.py >> /home/siegstedt/projects/machinemind/cron.log
As you can see, we add a “»” after the definition of the path to the Pyhton script. This will export the output to a log file. Then, you give the full file path including the name of the log file. By this you define where to store the file.

Let’s get to know each other.

Schedule a call and we can show you how we can boost your business.

This website uses cookies to ensure you get the best experience.

ARE YOU IN?

Many subscribers already enjoy our premium stuff. Subscribe now.