Create Multiple jobs using python-crontab
Last Updated :
02 Jun, 2022
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 allow us to access Cron, create jobs, establish limitations, and remove jobs, among other things. In this article, we will perform Multiple jobs using Python-Crontab.
Let's get started.
Installing Python-Crontab
First, we will need to make sure that we have python-crontab installed in your system. We can do so by typing the following command:
pip3 install python-crontab
Getting access to Crontab
We can get access to a crontab in five ways, three of which are system techniques that only operate on Unix-based operating systems and require the necessary permissions:
from crontab import CronTab
empty_cron = CronTab()
my_user_cron = CronTab(user=True)
users_cron = CronTab(user='username')
There are other two non-system methods that will work on Windows:
file_cron = CronTab(tabfile='filename.tab')
mem_cron = CronTab(tab=""" * * * * * command""")
Creating a New Job
We may use the following command to create a new cron job after we've accessed cron:
cron.new(command="cron_command")
For example, to create a cron job to run a python file temp.py every minute, we can do so by typing the below code:
from crontab import CronTab
cron = CronTab(user='root')
job = cron.new(command='python temp.py')
job.minute.every(1)
cron.write()
Creating Multiple jobs
Now let's use the above knowledge to create multiple jobs and run them simultaneously. For the purposes of demonstration, We have created 2 python files, namely test1.py and test2.py, which are used to log their access times on a file called test.txt
The following is the content of the python files
test1.py:
Python3
from datetime import datetime
import os
cwd=os.getcwd()
with open(os.path.join(cwd,"test.txt"),"a") as f:
f.write("Accessed test1.py on " + str(datetime.now()) + "\n")
test2.py:
Python3
from datetime import datetime
import os
cwd=os.getcwd()
with open(os.path.join(cwd,"test.txt"),"a") as f:
f.write("Accessed test2.py on " + str(datetime.now()) + "\n")
Now, We have created a file that is used to create cron-jobs whose main purpose is to run these python files at an interval of 2 minutes and 1 minute respectively. The following is the content of the python file.
Python3
from crontab import CronTab
cron=CronTab(user="aayussss2101")
job1=cron.new(command="python3 test1.py")
job1.minute.every(2)
job2=cron.new(command="python3 test2.py")
job2.minute.every(1)
cron.write()
Here is the content of the test.txt file after a few minutes after executing the above python code to create multiple jobs.
Similar Reads
Create a Credential file using Python A credential file is nothing but just a configuration file with a tad bit of encryption and an unseen security structure in the backend. There might be a situation where you might come across these kinds of files while using some kind of cloud platforms. All you do to login to the instance or give t
5 min read
Managing Cron Jobs Using Python Here, we will discover the significance of cron jobs and the reasons you require them in this lesson. You will examine python-crontab, a Python module that allows you to communicate with the crontab. What are Cron and Crontab?The utility known as Cron enables users to automatically run scripts, comm
6 min read
CRUD Operation on Oracle Database Using Python In this article, we will learn how to perform CURD operations on an Oracle Database by using Python. Oracle Database is a Database Management System produced and marketed by Oracle Corporation. It supports the Structured Query language (SQL) to Manage and Manipulate the Data. As a prerequisite, you
4 min read
Make multiple directories based on a List using Python In this article, we are going to learn how to make directories based on a list using Python. Python has a Python module named os which forms the core part of the Python ecosystem. The os module helps us to work with operating system folders and other related functionalities. Although we can make fol
3 min read
Profile Application using Python Flask and MySQL A framework is a code library that makes a developer's life easier when building web applications by providing reusable code for common operations. There are a number of frameworks for Python, including Flask, Tornado, Pyramid, and Django. Flask is a lightweight web application framework. It is clas
10 min read