Summary: To get a cron like scheduler in Python you can use one of the following methods:
Cron (also called a cron job) is a software utility that helps a user to schedule tasks in Unix-like systems. The tasks in cron are present in a text file that contain the commands to be executed for a scheduled task to be operational. The name of this file is crontab. To learn more about the corn scheduler, you can refer to this link.
In this article, we will focus on discussing how we can leverage the functions of a cron like scheduler in Python to manage scheduled jobs. So without further delay, let us jump into our mission-critical question:
Problem: Given a scheduled job; how to set a cron like scheduler for the job using Python?
Example: Given a text file (
test.txt) and a python script (
test.py). How to schedule a task in Python so that the Python script can be run at scheduled intervals?
The Python script is as follows:
from datetime import datetime myFile = open('test.txt', 'a') myFile.write('\nAccessed on ' + str(datetime.now())) myFile.close()
Upon execution of a certain scheduled task in Python, the desired output is:
Now that we have an overview of our problem statement, let us jump into the probable solutions:
Method 1: Using the schedule API
schedule is an in-process scheduler that provides a very user friendly syntax to schedule tasks using Python. Some of its key features include:
- Compatible with Python 2.7, 3.5, and 3.6.
- Simple syntax and easy to use API.
- No external dependencies.
schedule is not a part of the standard Python library, you have to install it using the following command:
$ pip install schedule
Let us have a look at the following program to see how we can use the
schedule module, to schedule tasks:
import schedule import time from os import system def job(): system('python test.py') # schedule the job to run at intervals of 1 min schedule.every(1).minutes.do(job) while True: schedule.run_pending() time.sleep(1)
Method 2: Using Advanced Python Scheduler
The Advanced Python Scheduler (APScheduler) is a lightweight and powerful task scheduler which helps us to run routine jobs. The key features of the APScheduler are:
- Does not include external dependencies.
- Available and tested on CPython 2.5 – 2.7, 3.2 – 3.3, Jython 2.5.3, PyPy 2.2
- Multiple, simultaneously active job stores – RAM, File-based simple database, SQLAlchemy, MongoDB, Redis.
- Thread-safe API
It provides three basic configurable mechanisms:
- Cron-like scheduling
- Delayed scheduling of single run jobs (like the UNIX “at” command)
- Interval-based (run a job at specified time intervals)
To be able to use the APScheduler, the apscheduler module must be installed since it is not a part of the regular Python library. Use the following command to install it:
$ pip install apscheduler
The following program demonstrates how we can use the APScheduler to run cron like jobs in Python (Please follow the comments in the code given below to get a better grip on the concept):
import time import os from apscheduler.schedulers.background import BackgroundScheduler def job(): os.system('python test.py') if __name__ == '__main__': # creating the BackgroundScheduler object scheduler = BackgroundScheduler() # setting the scheduled task scheduler.add_job(job, 'interval', minutes=1) # starting the scheduled task using the scheduler object scheduler.start() try: # To simulate application activity (which keeps the main thread alive). while True: time.sleep(1) except (KeyboardInterrupt, SystemExit): # Not strictly necessary but recommended scheduler.shutdown()
Method 3: Using the Timeloop Library
Another way of executing scheduled tasks is the timeloop library. If you are looking for something simple that can be implemented in your web or standalone application then timeloop could be a good choice. However, if you intend to work with complex operations then this library is not recommended.
Use the following command to install the
$ pip install timeloop
Let us have a look at the following code to understand how
from os import system import time from timeloop import Timeloop from datetime import timedelta tl = Timeloop() @tl.job(interval=timedelta(seconds=10)) def train_model(): system('python test.py') tl.start() while True: try: time.sleep(1) except KeyboardInterrupt: tl.stop() break
Method 4: Using The Crontab Module
crontab module uses a direct API for reading and writing crontab files and accessing the system cron automatically. Crontab is not a part of the standard Python library and has to be installed manually using the
The following syntax can be used to install the crontab module in your system:
$ pip install python-crontab
Let us understand how the crontab module works in a step-by-step approach:
Step 1: Getting Access To Crontab
There are five ways of accessing the crontab using the cron module in Python. Among these, three methods work in Unix based environments and require necessary permissions while the remaining two methods will work in Windows too.
The Unix specific methods are:
- cron = CronTab()
- cron = CronTab(user=True)
- cron = CronTab(user=’username’)
The two other ways that work for Windows as well are:
- file_cron = CronTab(tabfile=’filename.tab’)
- mem_cron = CronTab(tab=”””* * * * * command”””)
Step 2: Creating A New Job
creating a new job is very simple and can be don using the following command:
job = cron.new(command='/usr/bin/echo')
Step 3: Setting The Job Restrictions
crontab module provides us with the ability to set time restrictions upon the jobs without having to use cron’s syntax.
Job restrictions can be set using the following commands:
# to run the job every minute job.minute.every(1) # to schedule hourly jobs job.hour.every(4) # to run jobs on certain days of week job.dow.on('SUN', 'THU') # to schedule tasks/jobs on specific months job.month.during('APR', 'NOV')
Each restriction will clear the previous restriction. If you want to clear all job restrictions you can use the command:
Now let us have a look at the different options that we can use in the
crontab module (Please follow the comments to understand the significance of each command):
# enable a job: job.enable() # disable a job: job.enable(False) # to check if a task is enabled or disabled: job.is_enabled() # Chek whether a task is valid or not job.is_valid() # List all available cron jobs for job in cron: print job # Finding a cron job cron.find_command("command") # Find according to command cron.find_comment("comment") # Find according to comment cron.find_time(time schedule) # Find according to time # Removing a Job cron.remove(job) # Defining Environmental Variables job.env['VARIABLE_NAME'] = 'Value'
Now that we have an overview of the
crontab module and its functionalities, let us have a look at the following code to understand how it works:
from crontab import CronTab cron = CronTab(user='finxter') job = cron.new(command='python test.py') job.minute.every(1) cron.write()
Thus in this article, we learned various methods which can be used to get a cron like scheduler in Python. These were:
I hope you learned something from this article and it helps you in your coding journey. Please stay tuned for more interesting articles!
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.