Writing a crontab file in Python using the Plan Module

First, let us understand what a crontab file is. Cron is a software utility that helps us schedule tasks on Unix-based systems. You'll be able to periodically run assigned tasks, for example, automated system backups at the end of the day, auto shutdown, or set mode to DND at a particular time.

These "tasks" in cron are usually defined in a file called crontab, which is basically a text file that contains the commands to be executed.

Let us now learn how we can write these crontab files in Python using the plan module ?

Understanding Cron Syntax

Before we get into the implementation, let us first understand cron syntax. Cron syntax consists of 5 different fields separated by spaces ?

Minute Hour Day Month Day_of_week

The fields represent:

  • Minute (0-59)
  • Hour (0-23)
  • Day (1-31)
  • Month (1-12)
  • Day of week (0-6, where 0 = Sunday)

You can use special characters:

  • Comma to separate multiple values
  • Hyphen to specify a range
  • Asterisk (*) for all possible values
  • Forward slash (/) for step values

For example, 0 15 3,6 * * tells cron to run the task at 3 PM on the 3rd and 6th day of every month.

Installing the Python Plan Module

Now that you understand how cron works, let us install the required module. The plan module does not come pre-packaged with Python, so we'll install it using pip ?

pip install plan

Once installed, import it to your script ?

from plan import Plan

Creating Crontab Using the Plan Module

Let us start by creating an instance of the Plan class which will hold a set of cron jobs ?

from plan import Plan

# Create a Plan instance
cron = Plan()

Adding Commands and Scripts

Next, we can define commands or scripts that we want to run automatically. Let's assume we have a script that sends reminder emails for pending tasks every 5 hours, and we also want to print "hello" every 4 hours ?

from plan import Plan

cron = Plan()

# Add a command that runs every 4 hours
cron.command("echo hello", every="4.hours")

# Add a script that runs every 5 hours
cron.script("automation.py", path="/projects/scripts", every="5.hours")

# Write the crontab
if __name__ == "__main__":
    cron.run()

Other Scheduling Options

The plan module supports various scheduling formats ?

from plan import Plan

cron = Plan()

# Different scheduling options
cron.command("backup_script.sh", every="1.day", at="02:00")
cron.command("cleanup.py", every="1.week")
cron.command("report.py", every="monday", at="09:00")

if __name__ == "__main__":
    cron.run()

Complete Example

Here is a complete program that demonstrates creating a crontab file with multiple scheduled tasks ?

from plan import Plan

# Create Plan instance
cron = Plan()

# Schedule different tasks
cron.command("echo 'Daily backup starting'", every="1.day", at="02:00")
cron.script("send_reports.py", path="/home/user/scripts", every="monday", at="09:00")
cron.command("python /home/user/cleanup.py", every="1.week")

# Generate and run the cron jobs
if __name__ == "__main__":
    cron.run()

Key Features

Method Description Use Case
command() Execute shell commands System commands, one-liners
script() Run Python scripts Complex automation tasks
run() Generate crontab file Apply the scheduled jobs

Conclusion

The Python plan module provides an easy way to create crontab files programmatically. You can automate system backups, spell checks, coverage reports, or any recurring task using simple Python syntax instead of complex cron expressions.

Updated on: 2026-03-27T14:13:38+05:30

212 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements