The Wayback Machine - https://web.archive.org/web/20240710054309/https://www.geeksforgeeks.org/scheduling-python-scripts-on-linux/
Open In App

Scheduling Python Scripts on Linux

Last Updated : 31 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes we need to do a task every day, and we can do these repetitive tasks every day by ourselves, or we can use the art of programming to automate these repetitive tasks by scheduling the task. And today in this article we are going to learn how to schedule a python script on Linux to do the repetitive tasks.

We are going to a utility called cron to schedule the python script. Cron is driven by crontab which is also referred to as timetable because the word cron is derived from the Greek word Chronos which means time and tab is simply table.

Syntax: * * * * * command

In Crontab there are six fields. The first five are reserved for the date and time of scheduled execution, and the last field is reserved for a command to be executed.

Here is the python script that we are going to schedule:

Python3




#!/usr/bin/env python3
  
# importing libraries
import os
import random
  
# setting up folder name
folder_name = "geeksforgeeks"
  
# entering into the loop
# to create 2 folder every time this script runs
for i in range(2):
  
    # generating random number between 0 and 9
    number = int(random.randrange(0, 10))
  
    print("Creating folder {}".format(number))
  
    # creating directories
    os.mkdir(folder_name+" {}".format(number))


Output:

Scheduling Python Scripts on Linux:

Now following are the steps we need to be followed to schedule python scripts in Linux:

Step 1: Firstly, we have to create a python script that we will be going to schedule.  Above is the python script that we are going to use in this article.

Step 2: Open up the crontab to create a configuration file for scheduling the python script.

Step 3: Run the following command in the terminal to open up the crontab configuration file.

crontab -e

This should open up an editor to edit the configuration file and the output should look like this:

Step 4: Scroll to the end of the file and write down the timing and the command to be executed.

* * * * * /usr/bin/env python3 /home/amninder/Desktop/Geeks/cron/schedule.py >> /home/amninder/Desktop/Geeks/cron/output.txt

Here, “/usr/bin/env python3 /home/amninder/Desktop/Geeks/cron/schedule.py ” is the path to the script that we are going to schedule and “/home/amninder/Desktop/Geeks/cron/output.txt” is the path to the file where we are going to save our output. Asterisks (*) on all first 5 fields indicate that the script is going to execute after every minute, every hour.

To check logs to see if It’s working or not run the following command:

sudo tail -f /var/log/syslog

Output:

To remove the job from the crontab, run this command.

crontab -r : This will delete the current cron jobs.



Similar Reads

Command Line Scripts | Python Packaging
How do we execute any script in Python? $ python do_something.py $ python do_something_with_args.py gfg vibhu Probably that's how you do it. If your answer was that you just click a button on your IDE to execute your Python code, just assume you were asked specifically how you do it on command line. Let's make it easier for you. $ do_something $ do
4 min read
How to Schedule Python Scripts As Cron Jobs With Crontab
In this article, we will discuss how to schedule Python scripts with crontab. The Cron job utility is a time-based job scheduler in Unix-like operating systems. Cron allows Linux and Unix users to run commands or scripts at a given time and date. One can schedule scripts to be executed periodically. The crontab is a list of commands that you want t
3 min read
Top 12 Python Scripts For Developers to Implement
Python is a widely used high-level, general-purpose programming language. This language can be used in web development, machine learning applications, and all cutting-edge software technology and is preferred for both beginners as well as experienced software developers. Developers spent most of their time developing or working with scripts. Python
11 min read
Importing Python Functions from Other Scripts
Modular programming in Python enables reuse and structure of code. To accomplish this, importing functions from other scripts or modules is a potent method. This procedure facilitates collaborative development, promotes concern separation, and improves code readability. The ideas and procedures involved in importing Python functions from one script
3 min read
Running Extra scripts in Django
Running extra scripts or processes is always needed when you have some new idea that works with web development and in Python!!! it is always.It can be any script that may include a loading of data, processing, and cleaning of data, or any ML phase when making an application providing business logic directly in views or models is not always best. A
2 min read
How to handle form data in CGI Scripts?
In this article, we'll guide you through the process of creating a registration form in HTML and then using a CGI script to fetch and display the submitted user data in the form of a card, along with a success message. Registration forms are a fundamental component of many web applications, allowing users to provide essential information for access
4 min read
How To Enable or Disable CGI Scripts in Apache?
This article will guide you on how to enable or disable CGI scripts in Apache. Configuring CGI scripts in Apache is a crucial aspect of managing dynamic content generation on web servers. The Common Gateway Interface (CGI) provides a standardized protocol for executing programs, allowing websites to produce dynamic output. Whether enabling or disab
4 min read
How to generate webpages using CGI scripts?
In this article, we will explore the process of generating webpages using CGI scripts. Furthermore, we will discuss the importance of CGI script implementation in facilitating dynamic content creation. Additionally, we'll delve into the intricacies of linking one page to another by creating multiple pages within Python CGI scripts, ensuring a seaml
4 min read
How to generate basic HTML output using CGI scripts?
In this article, we will explore the process of generating basic HTML output using CGI scripts. Specifically, we will focus on creating a simple HTML web page using Python. The objective is to provide a step-by-step walkthrough, detailing the entire process of creating and utilizing CGI scripts to generate HTML content within a web environment. Wha
3 min read
How to Execute SQL queries from CGI scripts
In this article, we will explore the process of executing SQL queries from CGI scripts. To achieve this, we have developed a CGI script that involves importing the database we created. The focus of this discussion will be on writing code to execute SQL queries in Python CGI scripts. The initial step involves creating a database, and it's worth noti
5 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg