Handle and Set up Crontab in Python

| |

Home » Handle and Set up Crontab in Python

Cron is a Scheduling utility that can schedule scripts to be executed regularly. According to Wikipedia, the acronym stands for “chronological organizer”. It is written in the PERL programming language as a TSR (Terminate and Stay Resident) application that executes tasks at specified times. If you need to run anything at regular intervals or if you want to run anything automatically without any user intervention, cron jobs are the way to go. Whether you want to delete old files, send a certain email, transfer a file from one location to another, etc., cron jobs can be used to automate many tasks. This article will help you to set up crontab in Python.

A cron job is a command that is executed in the background by an operating system automatically. They are used for specific tasks like maintaining databases, cleaning unwanted files which can clog up your computer, or even triggering emails to be sent out at certain times. Crontabs are the constructs in which you will program your jobs. In this tutorial, you will learn about cron jobs and why they are necessary. Then, you will be introduced to the “crontab” system and how it can function in Python. By using the crontab module in Python, you will be able to alter and create cron jobs in your programs for whatever purpose you choose to use them.

How should you go about handling cron jobs with python?

When set up crontab in Python, it’s always a good idea to manage the cron daemon in an orderly manner. One example occurs when we need to schedule one command and then set the cron job without having to edit it manually. Python libraries or python-crontab are straightforward and effective methods of accessing the crontab in python code, providing tools that allow programmers to load cron jobs as objects, look for them, and save their actions. The tasks are specified by writing them into a file known as (at least with Python) a crontab, which has instructions regarding when those jobs are to be run. Cron will keep running after executing the task until it is forcibly terminated by faulty command or due to irrecoverable error.

Python-Crontab simplifies access to Crontab from your Python scripts or applications. You can monitor, manage or automate jobs from a running Python application that talks to the cron daemon. You will first need to install Python-crontab using ‘pip3’ to use cron; You can do that with:

pip3 install python-crontab

Creating a simple Cron Job

To get started, you’ll need to add a cron job to your crontab. Start a terminal and type in the following. The extra argument “-e” here is a reference to editing.

crontab -e

After entering the command you should see something similar to what’s shown in the following image:

Enter the following cron job at the very bottom of your crontab. This cronjob redirects the present date and time into a file “test.txt” in our case and saves it. After pasting the provided command below, save the using the ctrl+x key and then press enter.

* * * * * date > /home/linux/cron/test.txt  

Note: Replace ”linux” with the name of your PC or get the absolute path of your project file with the pwd command. To run it, go to the folder where you created your project file and run the following command in the terminal:


Now to explain what exactly this command actually does; it basically is executed every minute, showing us the date, time, and time zone. The 5 asterisk here represents that running the command means to execute itself every minute of every day of every week of every month.

Cron’s syntax is basically this:

.---------------- minute (0 - 59)
|  .------------- hour (0 - 23)
|  |  .---------- the day of the month (1 - 31)
|  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
|  |  |  |  .---- the day of week (0 - 6) (Sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat
|  |  |  |  |
*  *  *  *  *  command to be executed
FieldAllowed ValuesSpecial CharactersExtra Values
Minutes0-59* / , –< >
Hours0-23* / , –< >
Day1-31* / , –< >
Month1-12 or Jan-Dec* / , –< >
Day of week0-6 or Sun-Sat* / , –< >

To understand it more clearly you visit the Crontab Guru website and use their editor for cron schedule expressions to get the exact number combination that you need.

Now check if this is actually working by running another terminal window and use the watch command to show the contents of the test.txt file.

watch cat /home/linux/cron/test.txt  

You should get results somewhat similar to the following image:

Note: You may get a “No such file or directory”, that’s because cron jobs are not executed immediately, it may take a while for it to show the results.  

Here you can see, you get the date – Friday 15 October 2021, time 05:39:01 PM, and the time zone IST. You can see it in action if you can wait a minute and it increments by a minute automatically.

Scheduling a Python file with Crontab

With a good understanding of the command line and the principles behind executing cron commands, let’s move on and walk through one specific example. For simplicity’s sake, we’ll 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 the cron directory that we created called script_test.py with the following content:

import random
from datetime import datetime
now = datetime.now()
num = random.randint(1, 101)
with open('/home/linux/cron/script_test.txt', 'a') as f:  #write your own path here
  f.write('{} - The random number is {}\n'.format(now, num))

It is important in this case to run the script before we can use it as a cron job. Execute the script by going to the project file and execute it with:

python3 script_test.py

Check that the program worked or not with the cat.

cat /home/linux/cron/script_test.txt

If it worked properly then it should display something like this:

2021-10-15 19:38:43.900174 - The random number is 90

Now to add a cronjob to execute the script_test.py program every minute. Open the crontab file like before.

crontab -e

Add the appropriate path of your project file with the syntax in front:

* * * * * /home/linux/cron/script_test.py

Note: You must use absolute paths for everything in your crontab. 

Again. check if it works or not with the watch command.

watch cat /home/linux/cron/script_test.txt

And if it worked you’ll see a result like this:

Here it will keep updating every minute showing the date, time, and a random number; and this pretty much explains how cron jobs work and how you can set up crontab in Python. Now, you don’t want to keep this running all day, do you? You can clear the crontab with the following command which will reset anything you did in the crontab -e section.

crontab -r

Commonly occurring bugs 

Now, if you have followed the article precisely and did everything that’s told, you shouldn’t get any issues, but there are many gotchas! Moments when you set up crontab in Python. I have some of them in the following if you get stuck at any point.

Permission denied

Use chmod +x in front of the path of your file and run it in the terminal.

Script not working 

Add this  “#!/usr/bin/env python3” on the very top and it should fix it.

Anything not working, getting the wrong results

It’s the paths, check every path you have written, maybe some of them are broken or has a typo.

Here are some useful tutorials that you can read:


Basic TensorFlow Python Example to Get Started With

Get Weather Information using Python


Leave a Comment