Scroll to top

In this tutorial, you'll learn the importance of cron jobs and why you need them. You'll have a look at python-crontab, a Python module to interact with the crontab. You'll learn how to manipulate cron jobs from a Python program using the python-crontab module.

What Is Cron?

During system administration, it's necessary to run background jobs on a server to execute routine tasks. Cron is a system process which is used to execute background tasks on a routine basis. Cron requires a file called crontab which contains the list of tasks to be executed at a particular time. All these jobs are executed in the background at the specified time.

To view cron jobs running on your system, navigate to your terminal and type in:

1
crontab -l

The above command displays the list of jobs in the crontab file. To add a new cron job to the crontab, type in:

1
crontab -e

The above command will display the crontab file where you can schedule a job. Let's say you have a file called hello.py which looks like:

1
print("Hello World")

Now, to schedule a cron job to execute the above script to output to another file, you need to add the following line of code:

1
50 19 * * * python hello.py >> a.txt

The above line of code schedules the execution of the file with output to a file called a.txt. The numbers before the command to execute define the time of execution of the job. The timing syntax has five parts:

  1. minute
  2. hour
  3. day of month
  4. month
  5. day of week

Asterisks(*) in the timing syntax indicate it will run every time.

Introducing Python-Crontab

python-crontab is a Python module which provides access to cron jobs and enables us to manipulate the crontab file from the Python program. It automates the process of modifying the crontab file manually. To get started with python-crontab, you need to install the module using pip:

1
pip install python-crontab

Once you have python-crontab installed, import it into the Python program.

1
from crontab import CronTab

Writing Your First Cron Job

Let's use the python-crontab module to write our first cron job. Create a Python program called writeDate.py. Inside writeDate.py, add the code to print the current date and time to a file. Here is how writeDate.py would look:

1
import datetime
2
3
with open('dateInfo.txt','a') as outFile:
4
    outFile.write('\n' + str(datetime.datetime.now()))

Save the above changes.

Let's create another Python program which will schedule the writeDate.py Python program to run every minute. Create a file called scheduleCron.py.

Import the CronTab module into the scheduleCron.py program.

1
from crontab import CronTab

Using the CronTab module, let's access the system crontab.

1
my_cron = CronTab(user='your username')

The above command creates an access to the system crontab of the user. Let's iterate through the cron jobs, and you should be able to see any manually created cron jobs for the particular username.

1
for job in my_cron:
2
    print(job)

Save the changes and try executing the scheduleCron.py, and you should have the list of cron jobs, if any, for the particular user. You should be able to see something similar on execution of the above program:

1
50 19 * * * python hello.py >> a.txt # at 5 a.m every week with:

Let's move on with creating a new cron job using the CronTab module. You can create a new cron by using the new method and specifying the command to be executed.

1
job = my_cron.new(command='python /home/jay/writeDate.py')

As you can see in the above line of code, I have specified the command to be executed when the cron job is executed. Once you have the new cron job, you need to schedule the cron job.

Let's schedule the cron job to run every minute. So, in an interval of one minute, the current date and time would be appended to the dateInfo.txt file. To schedule the job for every minute, add the following line of code:

1
job.minute.every(1)

Once you have scheduled the job, you need to write the job to the cron tab.

1
my_cron.write()

Here is the scheduleCron.py file:

1
from crontab import CronTab
2
3
my_cron = CronTab(user='vaati')
4
job = my_cron.new(command='python3 /home/Desktop/vaati/writeDate.py')
5
job.minute.every(1)
6
7
my_cron.write()

Save the above changes and execute the Python program.

1
python scheduleCron.py

Once it gets executed, check the crontab file using the following command:

1
crontab -l

The above command should display the newly added cron job.

1
* * * * * python3 home/vaati/Desktop/writeDate.py

Wait for a minute and check your home directory, and you should be able to see the dateInfo.txt file with the current date and time. This file will get updated each minute, and the current date and time will get appended to the existing content.

Updating an Existing Cron Job

To update an existing cron job, you need to find the cron job using the command or by using an Id. You can have an Id set to a cron job in the form of a comment when creating a cron job using python-crontab. Here is how you can create a cron job with a comment:

1
job = my_cron.new(command='python3 home/vaati/Desktop/writeDate.py', comment='dateinfo')

As seen in the above line of code, a new cron job has been created using the comment as dateinfo. The above comment can be used to find the cron job.

What you need to do is iterate through all the jobs in crontab and check for the job with the comment dateinfo. Here is the code:

1
 my_cron = CronTab(user='vaati')
2
 for job in my_cron:
3
     print(job)

Check for each job's comment using the job.comment attribute.

1
 my_cron = CronTab(user='vaati')
2
 for job in my_cron:
3
     if job.comment == 'dateinfo':
4
         print(job)

Once you have the job, reschedule the cron job and write to the cron. Here is the complete code:

1
from crontab import CronTab
2
3
my_cron = CronTab(user='vaati')
4
for job in my_cron:
5
    if job.comment == 'dateinfo':
6
        job.hour.every(10)
7
        my_cron.write()
8
        print('Cron job modified successfully')

Save the above changes and execute the scheduleCron.py file. List the items in the crontab file using the following command:

1
crontab -l

You should be able to see the cron job with the updated schedule time.

1
* */10 * * * python3 /home/Desktop/vaati/writeDate.py # dateinfo

Clearing Jobs From Crontab

python-crontab provides methods to clear or remove jobs from crontab. You can remove a cron job from the crontab based on the schedule, comment, or command.

Let's say you want to clear the job with comment dateinfo from the crontab. The code would be:

1
from crontab import CronTab
2
3
my_cron = CronTab(user='vaati')
4
for job in my_cron
5
    if job.comment == 'dateinfo':
6
        my_cron.remove(job)
7
        my_cron.write()

Similarly, to remove a job based on a comment, you can directly call the remove method on the my_cron without any iteration. Here is the code:

1
my_cron.remove(comment='dateinfo')

To remove all the jobs from the crontab, you can call the remove_all method.

1
my_cron.remove_all()

Once done with the changes, write it back to the cron using the following command:

1
my_cron.write()

Calculating Job Frequency

To check how many times your job gets executed using python-crontab, you can use the frequency method. Once you have the job, you can call the method called frequency, which will return the number of times the job gets executed in a year.

1
from crontab import CronTab
2
3
my_cron = CronTab(user='vaati')
4
for job in my_cron:
5
    print(job.frequency())

To check the number of times the job gets executed in an hour, you can use the method frequency_per_hour.

1
my_cron = CronTab(user='vaati')
2
for job in my_cron:
3
    print(job.frequency_per_hour())

To check the job frequency in a day, you can use the method frequency_per_day.

Checking the Job Schedule

python-crontab provides the functionality to check the schedule of a particular job. For this to work, you'll need the croniter module to be installed on your system. Install croniter using pip:

1
pip install croniter

Once you have croniter installed, call the schedule method on the job to get the job schedule.

1
import datetime
2
3
sch = job.schedule(date_from=datetime.datetime.now())

Now you can get the next job schedule by using the get_next method.

1
print(sch.get_next())

Here is the complete code:

1
import datetime
2
from crontab import CronTab
3
4
my_crons = CronTab(user='vaati')
5
for job in my_crons:
6
    sch = job.schedule(date_from=datetime.datetime.now())
7
    print(sch.get_next())

You can even get the previous schedule by using the get_prev method.

Wrapping It Up

In this tutorial, you saw how to get started with using python-crontab for accessing system crontab from a Python program. Using python-crontab, you can automate the manual process of creating, updating, and scheduling cron jobs.

Have you used python-crontab or any other libraries for accessing system crontab? I would love to hear your thoughts. Do let us know your suggestions on the forum.

Learn Python

Learn Python with our complete Python tutorial guide, whether you're just getting started or you're a seasoned coder looking to learn new skills.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.