Crontab - Running a Python script with parameters
Last Updated :
18 Jul, 2021
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.
Similar Reads
Script management with Python Poetry Poetry is a tool that makes it easier to manage Python dependencies and packages and create virtual environments for a project, as well as to package and distribute Python libraries. Apart from dependency management, script management is also one of the strong features of Poetry where developers can
5 min read
Decorators with parameters in Python Prerequisite: Decorators in Python, Function DecoratorsWe know Decorators are a very powerful and useful tool in Python since it allow programmers to modify the behavior of functions or classes. In this article, we will learn about the Decorators with Parameters with the help of multiple examples. P
4 min read
How to Pass Parameters in URL with Python Passing parameters in a URL is a common way to send data between a client and a server in web applications. In Python, this is usually done using libraries like requests for making HTTP requests or urllib .Let's understand how to pass parameters in a URL with this example.Example:Pythonimport urllib
2 min read
How to Run a Python Script Python scripts are Python code files saved with a .py extension. You can run these files on any device if it has Python installed on it. They are very versatile programs and can perform a variety of tasks like data analysis, web development, etc. You might get these Python scripts if you are a begin
6 min read
How to run bash script in Python? If you are using any major operating system, you are indirectly interacting with bash. If you are running Ubuntu, Linux Mint, or any other Linux distribution, you are interacting with bash every time you use the terminal. Suppose you have written your bash script that needs to be invoked from python
2 min read