Ramblings

Python – Schedule Job – Like Cronjob

By July 31, 2022 No Comments

Cron is an incredible scheduler that is extremely versatile with a powerful scheduling engine and precise timing capability, but it’s typical run from a server’s root OS or container image. You could package a linux container with the cronjob task and a shell script as shown at the end of this post OR you can build your own python scheduled job and have advanced python scripts and fluent syntax of the schedule module as shown here.

Follow the steps from this post to create a python virtual environment and ensure the outputted schedule module is in the requirements.txt file.

pip install schedule
pip freeze > requirements.txt

The following src/scheduler.py file shows a few examples of creating jobs and scheduling them to run at different intervals. The fluent syntax allows you to string together readable events to be triggered.

# Schedule Library imported
import schedule
import time

print("[starting scheduler]")

# Tasks
def heartbeat():
    print("still alive...")


def do_every_minute():
    print("every minute")


def do_every_hour():
    print("every hour")

def do_every_Sunday():
    print("every Sunday")

def do_specific_day_and_time():
    print("every Sunday")

# Task scheduling
schedule.every().second.do(heartbeat)
schedule.every().minute.do(do_every_minute)
schedule.every().hour.do(do_every_hour)
schedule.every().sunday.do(do_every_Sunday)
schedule.every().monday.at("3:30").do(do_specific_day_and_time)

# Loop so that the scheduling task - keeps on running all time.
while True:
    schedule.run_pending()
    time.sleep(1)

Alternatively create a Dockerfile for the solution to deploy the scheduler as a container. Note that your do.sh file contains your shell script that will be invoked. If you need to know more about cron job scheduling and the * * * * * syntax, check out https://crontab.guru/

# Dockerfile to create image with cron services
FROM ubuntu:latest

# Add the script to the Docker Image
ADD do.sh /root/do.sh

# Give execution rights on the cron scripts
RUN chmod 0644 /root/do.sh

#Install Cron
RUN apt-get update
RUN apt-get -y install cron

# Add the cron job
RUN crontab -l | { cat; echo "* * * * * bash /root/do.sh"; } | crontab -

# Run the command on container startup
CMD cron