Open In App

Crontab – Running a Python script with parameters

Last Updated : 18 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Scheduling python scripts with crontab is fundamental when it comes to automating tasks using python. We will see how to schedule python scripts and pass the necessary parameters as well.

The Cron job utility is a time-based job scheduler in Unix. It allows the user to run the file at a given time and date. And crontab is a list of commands that you want to run on a regular schedule, and also the name of the command used to manage that list. 

Creating Python script for demonstration:

First, let’s create a simple script that we will be scheduled to run every 2 minutes. The below is a simple script that calculates the product of all parameters passed and prints them to STDOUT along with the time the script was run.

Python




#! /usr/bin/python3
  
import sys
from datetime import datetime
  
def main(args):
    ans = 1
    for arg in args[1:]:
        ans *= int(arg)
    print("calculated result as: {} on: {} ".format(ans,
                                                    datetime.now()))
  
if __name__ == '__main__':
    main(sys.argv)


Note: #!/usr/bin/python3 (specifying the path of script interpreter) is necessary if you wish to make the script executable.

Assuming we have saved this script as  my_script.py under our home directory, we can make it executable by entering the following command in our terminal:

$ sudo chmod +x my_script.py

We can test our script if it is working properly.

./my_script.py 1 2 3 4 5
calculated result as: 120 on: 2021-07-01 12:19:48.856184

The crontab scheduling expression has the following parts:

Crontab also accepts special characters for creating a more complex time schedule:

Character Meaning
Comma To separate multiple values
Hyphen To indicate a range of values
Asterisk To indicate all possible values
Forward slash To indicate EVERY

To schedule our script to be executed, we need to enter the crontab scheduling expression into the crontab file. To do that, simply enter the following in the terminal:

crontab -e

You might be prompted to select an editor, choose nano and append the following line to the end of the opened crontab file:

*/2 * * * * /home/$(USER)/my_script.py 1 2 3 4 5 >> /home/$(USER)/output.txt

where $(USER) can be replaced with your username. Save changes and exit. This will schedule our Python script to run every 2 minutes with 1 2 3 4 5 as command-line arguments and write the output to /home/$(USER)/ouput.txt.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads