Let’s talk about an interesting scheduling module today – Python crontab. Something worthy of mention would be that the support for cron is quite lacking in POSIX, i.e. Windows Operating System. As such, the examples in this article use Linux.
Introduction to Python crontab
If you’ve worked with the datetime module, or the sched module, it’s safe to say that you’ve wanted to schedule an alert at some point in time.
If you’ve pondered on how the expansion of such a feature would persist, you’ve also maybe come to a conclusion where one could write a script that can deploy the same event continually, and repetitively.
Simplifying all that, you might’ve come to an idea, or a question, how about I automate my task?
Well, good news.
It’s easily possible! cron is a feature that allows for scheduling commands, and thus, helping in running commands at a particular interval or time.
What is cron?
A feature that’s existed in UNIX-like operating system, is the time-based job scheduler that is, cron.
It’s used in software development environments in order to schedule jobs which can run periodically, at fixed times, dates, or intervals that you can set up for yourself.
Syntax of Python crontab
Cron would require an entire article to itself in order to explain, so, here’s an article that can help you get an idea of what we’re going to be working with here.
We will be working with crontabs, which contains all the jobs which we’ve scheduled or are going to be scheduling.
If you face any issue with the creation of the cron task, you should try out a few online tools that can help you figure out the syntax. Check out crontab.guru to create your task in case you face any issues.
Working with the Python crontab module
python-crontab module is one which allows for the process of creation of cron jobs to get a lot more simpler.
It provides us with a simple class taking direct input, without us having to work with the cron syntax at all.
In order to work with cron, and crontab in Python, we’ll first need to install the required module, this can be done with the pip package manager command in the shell.
pip install python-crontab
This should automatically install the required module, and once you’re done with it, we should be ready to work with it!
Let’s get right into the working of the module here and schedule our first task.
1.0 Setting up
Before we get started with working with the expressions and the tasks in Crontab, we’ll first need to import the required module.
# Importing the CronTab class from the module from crontab import CronTab
1.1 Object Creation
In order to work with the Python crontab, we’ll need to set up an object to work with creating jobs and their recurrence.
# Creating an object from the class ## Using the root user cron = CronTab(user="root") ## Using the current user my_cron = CronTab(user=True) # Creating an object from the class into a file file_cron = CronTab(tabfile="filename.tab")
1.2 Working with Jobs
python-crontab module, we can create jobs, and specify when we want them to repeat, and what interval they must reoccur in.
This module simplifies a large portion of creating these tasks and turns it from functional input into a crontab.
# Creating a new job job = cron.new(command='echo hello_world') # Setting up restrictions for the job ## The job takes place once every 5 minutes job.minute.every(5) ## The job takes place once every four hours job.hour.every(4) ## The job takes place on the 4th, 5th, and 6th day of the week. job.day.on(4, 5, 6) # Clearing the restrictions of a job job.clear()
Keep in mind that every single time you change the restriction on the job, the job wipes and replaces itself with the new restriction.
1.3 Writing to the crontab file
In the end, we create these jobs to provide us with the cron form of the given restrictions, and in order to write to the file, we must manually order the object to write itself into the file.
This can be performed through a simple command at the end of setting up the job restrictions.
Looking back, you’ll find that cron is the name of our object created from the
1.4 CronTab file
Executing the Python file every time you set a new restriction, a clean CronTab should look something like this.
*/5 * * * * echo hello_world */5 */4 * * * echo hello_world */5 */4 4,5,6 * * echo hello_world
Working with cron is a big step ahead in automating processes, and in case you wish to look into modules that can help in deciding the time and date to set up tasks and jobs, you might be interested in our other articles!