Open In App
Related Articles

Crontab – Running a Python script with parameters

Like Article
Save Article
Report issue

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.


#! /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,
if __name__ == '__main__':


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 under our home directory, we can make it executable by entering the following command in our terminal:

$ sudo chmod +x

We can test our script if it is working properly.

./ 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:

CommaTo separate multiple values
HyphenTo indicate a range of values
AsteriskTo indicate all possible values
Forward slashTo 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)/ 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.

Last Updated : 18 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Similar Reads