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:

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

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:

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.



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.

Similar Reads

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....

Create Your First Cron Job in Crontab

Here are some examples of the Crontab commands:...

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:...

Contact Us