Tuesday, November 19, 2024
Google search engine
HomeLanguagesManaging Cron Jobs Using Python

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, commands, or applications according to a predetermined schedule. The file called Crontab contains a list of the jobs that will be run by Corn.

CronJobs are used widely used in industries to schedule some important and mandatory jobs automatically like Scraping stock data, Updating the database at some intervals, generating and sending reports, etc.

Run the following command for installation:

pip3 install python-crontab

install-crontab

Install python-crontab

After the package is installed we can now use it to manage the cronjobs – create new cronjobs, update existing cronjobs or delete cronjobs. To import it to our Python program we can do this:

from crontab import CronTab

Schedule a Task With Cron

Cron uses schedule expressions to know when you want to execute a task. Specification of the schedule of the CronJobs is according to cron utility. It is specified by this kind of symbol:

* * * * *

Starting from the left-most, the first star represents the minute (between 0 to 59), the second star represents the hour (between 0 to 24), the third represents the day of the month, the fourth represents the month of the year and the fifth represents the day of the week.

CRON Schedule Expressions

The job frequency can be calculated using the cron table format and how is it set for the cron job. A few examples to explain this would be:

  • */2* * * * * : This will run every 2 minutes
  • 0 */8 * * 1-6 : This will run every 8 hours from Monday to Friday
  • * * * jan,feb * : Run only in the month of January and February
  • 0 4 * * * : Run at 4 am everyday

Create Your First Cron Job in Crontab

Here are some examples of the Crontab commands:

crontab -l # lists all cronjobs

crontab -e # edit cronjob

crontab -r # delete cronjobs

Clearing jobs from Crontab

We can clear all cron jobs using crontab with just a single command:

crontab -r

This is demonstrated in the following image

Cron Jobs Using Python

Removing cron jobs

Checking Job Schedule

The schedule of the cronjobs can be found by running the following command:

crontab -l

It gives all the details of the cron job including name, schedule, and command.

Cron Jobs Using Python

Checking job schedule

Checking the validity of a cron job

Cron logs each attempt to execute a command in Syslog. We can validate that our job is scheduled properly by grepping syslog for the name of the command. We just need to run this command:

grep /home/Desktop/freeMemory.py /var/log/syslog

This will return a good response if the cron job was valid and executed without any error and will return an error if it failed for some reason. This also refreshes the output after the job runs every time.

Cron Jobs Using Python

Grep syslog for cron job validity

Create a new Cronjob with Python using subprocess

For example, we want a job that tells how much memory is being used and how much is free at that moment after every 1 hour. We first need to create a job file that will execute this:

Python3




import subprocess
//command to be executed by the job
command = 'free'
//storing output of command to a file
with open('memoryInfo.txt', 'a') as outputFile:
    outputFile.write(
        '\n' + str(subprocess.check_output(command).decode('utf-8')))


This code uses the “subprocess” module in Python. This module helps us create/spawn new processes and run them – it is mainly used if we want to run some Linux commands inside Python code. In this program, we want to run the “free” Linux command. This command lets us know the memory statistics in our system at that moment. So, we run this command using the subprocess module and store the output of this in a file -“memoryInfo.txt”.

The function used for running the Linux command – check_output(command). This takes in the parameter as the command string and returns the standard output of the command after running it.

The following is what it will write to the file:

Screenshot-from-2023-06-17-21-58-37.png

Job Output

Create a Python Script Scheduler

Now, to schedule this job using the python-crontab package we can do the following:

Python3




from crontab import CronTab
//creating a CronTab instance with the user
jobScheduler = CronTab(user='vrinda')
//creating a new cron job with the scheduler
job = jobScheduler.new(command='python3 /home/Desktop/freeMemory.py')
//schedule the job
job.hour.every(1)
jobScheduler.write()


This code basically creates a cronjob for the previous code using the crontab module in Python. This Python module makes it a lot easier to work with cron jobs.

First, we create an instance of the CronTab with the user we want (either the root or the current user). This instance is then used to create jobs. We create a new job with new(command) method. This creates a job that will run the command we specified. After creating the job, we schedule the job as per our needs. Here, we schedule the job to run every 1 hour with this – job.hour.every(1). If we want to specify something like every 20 minutes we can use this – job.minute.every(20). Finally, we need to write this cron job to the system so that it actually runs so for that we use the write() method. After we execute this Python script, the job is then scheduled and can be verified by the Linux crontab command:

crontab -l

Cron Jobs Using Python

Crontab command

So, we have successfully created a cronjob using the python-crontab package in Python.

Updating an Existing Cron Job

For editing an existing cronjob we need to identify it by its name or an id. So for an id, we can add a comment to our existing job with this:

job.set_comment("Freememory job")

Now, to edit the existing job from running every hour to every minute we can do the following:

Python3




from crontab import CronTab
jobScheduler = CronTab(user='vrinda')
job = jobScheduler.new(command='python3 /home/Desktop/freeMemory.py')
//adding comment on the job
job.set_comment("Freememory job")
//looking for the job using the comment
for job in jobScheduler:
     if job.comment == 'Freememory job':
        job.minute.every(2)
        jobScheduler.write()


In this code, we again use the crontab module like in the previous example. But this time we use it for editing a job. For modifying a job, we need an identifier to choose the job to modify. For that, we use comments in cronjob.

We set a comment for the job using the set_comment(“comment”) method with the comment string as the parameter. This adds a comment to the job. Now, we loop through all the jobs that exist under that user and if the comment matches, we modify the job schedule to run every 2 minutes. Then finally, write back the job to update it. After we run this, we can verify the cronjob schedule actually changed:

Cron Jobs Using Python

Editing job

Similarly, we can do a lot of other things as well like removing the jobs, checking the schedule and frequency of the jobs, etc with this Python package.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments