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 the Unix based systems. So basically, you’ll be able to periodically run assigned tasks, for example, automated system backups at the end of the day, to auto shutdown or set mode to DND at a particular time and more such features.

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!

Getting Started

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

Minute Hour Day Month Day_of_week
Minute (0-59), Hour (0-23), Day (1-31), Month (1-12) and Day of week (0-6). 

You can also use comma to separate values and include more than one value. Hyphen to validate a range. Asterisk for all possible values and finally forward slash to indicate everything.

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

Alright, now that you understand how cron works, let us get started by installing the required module, plan.

This module does not come pre−packaged with Python. So, we’ll be downloading and installing it using the pip package manager.

To install the plan module, use the following command:

pip install plan

And now, we can import it to our script using the following command:

from plan import Plan

You are all set to start creating cron files.

Creating crontab Using the plan Module

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

cron = Plan()

Next up we can just go ahead and define functions or scripts or commands you want to automatically run.

Let us assume we have a script that automatically sends a reminder mail for tasks pending every 5 hours. Let us say this script is located at "/projects/scripts/automation.py".

We also want it to print hello every 4 hours. So how do we do this?

We have different ways of doing this.

We can use the cron.command() function or the cron.script() function. Both of these commands will help reach our goal.

cron.command("echo hello", every= "4.hours")
cron.script("automation.py", path= "/projects/scripts" every= "5.hours")

Now, let us run the commands by using the cron.run() command in our main function.

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

And that’s it! You have now automated the steps and defined a fixed time at which the scripts defined runs.

Example

Here is the complete program:

from plan import Plan

cron = Plan()
cron.command("echo hello", every= "4.hours")
cron.script("automation.py", path= "/projects/scripts" every= "5.hours")
If __name__ == "__main__":
	cron.run()

Conclusion

You now understand how to automate running of scripts at fixed time. Using this you can automate spell checks in your projects, automate system back ups at specific times or even get coverage report of your project everyday.

Updated on: 31-Aug-2023

48 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements