How To Get A Cron Like Scheduler in Python?

Rate this post

Summary: To get a cron like scheduler in Python you can use one of the following methods:

  1. Use schedule module
  2. Use APScheduler
  3. Use timeloop library
  4. Use CronTab module

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 ( 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(

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:

  1. Compatible with Python 2.7, 3.5, and 3.6.
  2. Simple syntax and easy to use API.
  3. Lightweight.
  4. No external dependencies.

Since 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():

# schedule the job to run at intervals of 1 min

while True:


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:

  1. Does not include external dependencies.
  2. Available and tested on CPython 2.5 – 2.7, 3.2 – 3.3, Jython 2.5.3, PyPy 2.2
  3. Multiple, simultaneously active job stores – RAM, File-based simple database, SQLAlchemy, MongoDB, Redis.
  4. 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():

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

        # To simulate application activity (which keeps the main thread alive).
        while True:
    except (KeyboardInterrupt, SystemExit):
        # Not strictly necessary but recommended


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 timeloop library.

$ pip install timeloop

Let us have a look at the following code to understand how timeloop works:

from os import system
import time
from timeloop import Timeloop
from datetime import timedelta

tl = Timeloop()

def train_model():


while True:
    except KeyboardInterrupt:


Method 4: Using The Crontab Module

The 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 pip command.

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:

  1. cron = CronTab()
  2. cron = CronTab(user=True)
  3. cron = CronTab(user=’username’)

The two other ways that work for Windows as well are:

  1. file_cron = CronTab(tabfile=’’)
  2. 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  ='/usr/bin/echo')

Step 3: Setting The Job Restrictions

The 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
# to schedule hourly jobs
# 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:
# disable a job:
# to check if a task is enabled or disabled:
# Chek whether a task is valid or not
# 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
# 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 ='python')


Thus in this article, we learned various methods which can be used to get a cron like scheduler in Python. These were:

  1. Using schedule
  2. Using APScheduler
  3. Using timeloop
  4. Using crontab module

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!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. 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?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

๐Ÿš€ If your answer is YES!, consider becoming 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.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar โ€œHow to Build Your High-Income Skill Pythonโ€ and learn how I grew my coding business online and how you can, tooโ€”from the comfort of your own home.

Join the free webinar now!