How to Schedule a Python Script with Crontab

In this tutorial, you’ll learn how to schedule a Python script using crontab.  Don’t worry if you never used crontab before.  There are no prerequisites for this tutorial. I’ll walk you through everything you need to know step-by-step with lots of examples along the way.  Here we go!

Schedule a Python script with crontab

Crontab vs Cronjob – What’s the Difference?

A crontab is a file which contains the schedule of cronjob entries to be run at specified times.  Crontab is short for cron table.  You can think of a crontab as a configuration file that specifies shell commands to run periodically on a given schedule.  A cronjob is basically instructions to run a command at specific times on specific dates.

Crontab Syntax

The crontab syntax is very powerful and flexible.  Below is a reference describing the syntax for a cronjob i.e. a single line in the crontab file.

┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of the month (1 - 31)
│ │ │ ┌───────────── month (1 - 12)
│ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
│ │ │ │ │                                   7 is also Sunday on some systems)
│ │ │ │ │
│ │ │ │ │
* * * * * command to executeFrom left to right, each of the 5 asterisks represents minute, hour, day of month, month, and  day of week.  Finally on the very right is the actual command to execute.

A Very Simple Cronjob Example

To add a cronjob to your crontab, open up a terminal window and type the following. The -e argument here means edit.

crontab -e

Assuming that you have no cronjob entries, you’ll see an empty file.  Type the following cronjob into your crontab.  This cronjob will redirect the current date and time into a file. Save and close your file when you are done.

* * * * * date > /tmp/test.txt

If you guessed that this command will be executed once per minute, you’re absolutely right. A series of five asterisks in a cronjob is quite valid and simply means to execute the command every minute of every day of every week of every month.

Let’s prove that this is actually working.  In another terminal window, let’s use the watch command to periodically cat the contents of the test.txt file to the screen.

watch cat /tmp/test.txt

If you’re quick enough, you may see a “No such file or directory” error if the cronjob hasn’t been executed yet. Otherwise, you will see the contents of test.txt similar to below.

Mon Oct 7 12:21:00 CEST 2018

Wait another minute and you will see the date increment by a minute.

Mon Oct 7 12:22:00 CEST 2018

It’s worth pointing out here that cronjobs are by default executed at the top of the minute.

More Examples of Cronjobs

Now that you have created your very first cronjob, let’s go over some other cronjob examples that will execute at various frequencies.

8 Cronjob Examples to Help You Better Understand Crontab

DescriptionCrontab Syntax
Every day at midnight0 0 * * *
Every day at 3:30 PM30 15 * * *
Every hour0 * * * *
Monthly at midnight on the first day of the month0 0 1 * *
Midnight on the 1st ,10th, and 15th of every month0 0 1,10,15 * *
Every weekday at 8:01 PM1 20 * * 1-5
Midnight on the 15th of March, June, September, and December0 0 15 3,6,9,12 *
Every year at midnight on April 25th0 0 25 4 *

As you can see, the crontab syntax is very flexible.  You can pretty much execute any command on any schedule no matter how simple or complicated it may be.

Schedule a Python Script with Crontab

With a good understanding of crontab and the cronjob syntax under your belt, let’s move on and walk through an example to schedule a Python script using crontab.

For simplicity’s sake, let’s write a simple Python program to log the date, time, and a random number between 1 and 100 to the end of a file. Create a file in your home directory called rand.py with the following content.

import random
from datetime import datetime
now = datetime.now()
num = random.randint(1, 101)
with open('/tmp/rand.txt', 'a') as f:
f.write('{} - Your random number is {}\n'.format(now, num))

Let’s test out this program before we add it as a cronjob. Execute the following in a terminal window.

python rand.py

Check that the program did its job with cat.

cat /tmp/rand.txt

You should see something similar to below.

2018-10-07 12:33:21.211066 - Your random number is 65

It works! Before we add a cronjob to execute the rand.py program every minute, you must know that it is necessary to use absolute paths for everything in your crontab. This is where most people get hung up with cronjobs.  We will need the absolute path of the Python binary and the absolute path of our Python script.

  1. To get the absolute path of the Python binary, execute which python in a terminal window. In my case, the Python binary is at /usr/local/bin/python
  2. To get the absolute path of your Python script, execute the pwd in the same directory as the rand.py program.  In my case, my Python script is at /Users/tonyflorida/rand.py

Alright. Now that we know where everything resides on our filesystem, let’s schedule our Python script to execute every minute.  Open the crontab file like before.

crontab -e

Add the following line to the bottom of your crontab, substituting the appropriate paths for your filesystem.  Save and close your file when you are done.

* * * * * /usr/local/bin/python /Users/tonyflorida/rand.py

Again, we can use the watch command to monitor the contents of the /tmp/rand.txt file.

watch cat /tmp/rand.txt

After a few minutes, your rand.txt file will look similar to this.

2018-10-01 13:57:31.158516 - Your random number is 27
2018-10-01 14:01:00.175556 - Your random number is 23
2018-10-01 14:02:00.267484 - Your random number is 81
2018-10-01 14:03:00.386802 - Your random number is 85
2018-10-01 14:04:00.504855 - Your random number is 22
2018-10-01 14:05:00.613324 - Your random number is 94
2018-10-01 14:06:00.706200 - Your random number is 45

Crontab Scheduling Final Thoughts

Congratulations! You now know how to schedule a Python script using crontab. I’m sure by now, you realize the endless possibilities of scheduling tasks with crontab.

Let’s clear out all of our cronjobs with the following command.

crontab -r

Any questions about how to schedule a Python script with crontab, let me know in the comments below.  For another Python tutorial, check out last week’s post about how to send an email from Python.

About The Author

With a strong software engineering background, Tony is determined to leverage the internet to positively impact as many people as possible. Discover why Tony quit his dream job to pursue this mission. You can send Tony a message here.

Leave a Comment

shares