Cron Like Schedular in Python

Cron Like Schedular in Python

  1. the cron and scheduler Modules in Python
  2. Schedule Jobs With schedule Module

This tutorial will explain scheduling the tasks similar to the cron job in Linux. First, we will look at the schedule module to schedule our jobs in an in-process schedular.

the cron and scheduler Modules in Python

Python provides us with several packages that allow us to schedule tasks automatically. Those packages include schedule and time.

The cron is used for scheduling and automating our tasks. For example, after a specific time interval, a particular job automatically starts executing.

We often need to perform many tasks periodically without requiring manual interventions. We can achieve this by using a task schedular.

Scheduling often helps and facilitates us in managing our data stored in databases. It also allows us to fetch our data periodically as per the need.

The schedule module in Python is a sophisticated scheduling module that schedules our tasks to run as per the configurations.

The cron is a time-based scheduler. It helps us to schedule those jobs which have to be performed periodically. The crontab file, a part of the Linux cron utility, contains the list of scheduled tasks.

To schedule our tasks in cron, we can directly edit this file using the Bash command crontab -e or the python-crontab module of Python. But, we can’t use these scheduling schemes in Windows operating systems.

The schedule is a library that works according to the time interval of your system. It serves as an in-process schedular and works on almost any operating system.

It helps us schedule certain tasks at different time intervals, like a specific time in a day or week. So, let’s start by creating a new python project.

Schedule Jobs With schedule Module

Follow the instructions given below to create a new Python project:

  • The first step is to install the anaconda prompt.
  • Then type Jupyter notebook on the prompt screen.

    cron like schedular in python - anaconda prompt

  • You will get this home page of Jupyter notebook on the browser.

    cron like schedular in python - home page

  • Click on New, then make a Python 3 file.

    cron like schedular in python - new python file

  • Python file will appear as follows.

    cron like schedular in python - python file

  • Before using any of the functionalities of the schedule module, we need to install the schedule module first.
    pip install schedule
    
  • After importing the required module, let’s look at a code to create and schedule a job.
    import schedule
    import time
    
    def job():
       print("Reading time...")
    
    def coding():
       print("Programming time...")
    
    def playing():
       print("Playing time...")
    # Time
    schedule.every(5).seconds.do(job)
    schedule.every(2).minutes.do(coding)
    schedule.every().day.at("10:57").do(playing)
    while True:
       schedule.run_pending()
       time.sleep(1)
    

    There are three different jobs defined in this code. The first job, reading time, repeats itself every 5 seconds, the second job, coding, will repeat itself every 2 minutes, and the last job will repeat itself when the specific time arises.

    The Schedule.run_pending() function in the while loop checks if there is any scheduled job pending to run or not. When executed, the function time.sleep(1) will delay the schedule to check its pending tasks for one second.

    Output:

    cron like schedular in python - output

    We can also ask the scheduler to perform a specific job after hours and minutes. For example, the schedule.every(5).hours.do(job) statement will repeat the job after every five hours.

Related Article - Python Cron

  • Execute Python Script via Crontab