Crontab - Running a Python script with parameters Last Updated : 18 Jul, 2021 Comments Improve Suggest changes Like Article Like 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: CharacterMeaningCommaTo separate multiple valuesHyphenTo indicate a range of valuesAsteriskTo indicate all possible valuesForward 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)/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. Comment More infoAdvertise with us Next Article Crontab - Running a Python script with parameters ayush.verma16 Follow Improve Article Tags : Python python-utility Practice Tags : python 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 How to Run Python Script in GitHub Actions ? A tool available on GitHub that can help you automate chores in your software projects is called GitHub Actions. It enables you to design workflows that, when executed automatically, carry out actions like as deploying, testing, and even sending out notifications. It basically works like a small rob 6 min read How to check any script is running in linux using Python? Python is a strong and exponentially growing programming language in the present day. There is a multiple-way to check which script is running in the background of a Linux environment. One of them is using the subprocess module in python. Subprocess is used to run new programs through Python code by 2 min read How to Run a Python Script using Docker? Docker helps you to run your Python application very smoothly in different environments without worrying about underlying platforms. Once you build an image using dockerfile you can run that image wherever you want to run. Docker image will help you to package all the dependencies required for the a 8 min read Create Multiple jobs using python-crontab Cron is a Unix-like operating system software utility that allows us to schedule tasks. Cron's tasks are specified in a Crontab, which is a text file that contains the instructions to run. The Crontab module in Python allows us to handle scheduled operations using Cron. It has functionalities that a 2 min read How to setup Notepad to run Python Script Due to its simplicity, versatility, and readability, Python has become phenomenally popular as a computer language that is highly leveled and dynamically typed. These are some of the IDEs for Python development that have many tools; however, if you are just starting or want it lighter, then Windowsâ 2 min read Like