DEV Community

Cover image for Linux Job Scheduling using Python
Subhendu Dash
Subhendu Dash

Posted on • Updated on

Linux Job Scheduling using Python

Have you ever considered automating basic tasks on your desktop, such as sending timely emails to friends or downloading files from a certain source?

Well, it's possible if you know job scheduling in operating systems.

In this article, I will show how to schedule a job in Linux (I will be using Ubuntu 22.04 here).

Cron

In Linux, "cron" command utility is used for job scheduling. Cron is derived from a Greek word "Chronos" meaning time. All the cron along with the task to be done, is stored in a crontab file.

The syntax of cron is given below

 ┌───────────── minute (0 - 59)
 │ ┌───────────── hour (0 - 23)
 │ │ ┌───────────── day of the month (1 - 31)
 │ │ │ ┌───────────── month (1 - 12)
 │ │ │ │ ┌───────────── day of the week (0 - )
 │ │ │ │ │                                 
 │ │ │ │ │
 │ │ │ │ │
 * * * * * <command>
Enter fullscreen mode Exit fullscreen mode

For example, if you want to represent "25 May 18:30", then it's cron representation will be

30 18 25 5 *
Enter fullscreen mode Exit fullscreen mode

Using Crontab in Ubuntu

Let's run a Python script automatically using Crontab. I will write a Python program to make a simple GUI app using tkinter module.

from tkinter import *

window = Tk(className="App")
window.geometry("300x300")

label = Label(window, text="Hello World")
label.pack()

window.mainloop()
Enter fullscreen mode Exit fullscreen mode

Now save this Python script in a directory and copy the file location.

Open terminal and type

crontab -e
Enter fullscreen mode Exit fullscreen mode

This command will open Crontab file in that terminal (vim/nano/emacs editor based on your preference. I am using nano.).

crontab file opened in nano

Remove all the comments if you want.
Add an extra line in that file

43 13 25 5 * /usr/bin/python3 -u /path/of/file/script.py
Enter fullscreen mode Exit fullscreen mode

Note: Specify the path of python3 interpreter (usr/bin/python3 in Ubuntu) while entering a job.
Execute whereis python3 in your terminal to get the path.

So, you will get a new window automatically on time.

Output

There are many useful crontab commands, I will list out here.

crontab -e // opens a new crontab file
crontab -l // shows list of job scheduled
crontab -r // deletes user's crontab
crontab -h // to display list of commands available in crontab 
Enter fullscreen mode Exit fullscreen mode

Accessing Crontab using python-crontab module

Opening Crontab files and entering the jobs manually is a tedious task.
There is a module in Python python-crontab which helps to
schedule the job easily.

Install python-crontab.

pip3 install python-crontab
Enter fullscreen mode Exit fullscreen mode

Create a new Python file. Import and initialise Crontab object with current user

from crontab import CronTab
cron = CronTab(user="<username>")
Enter fullscreen mode Exit fullscreen mode

Note: Type whoami in terminal to get the username.

Set a new command using cron.new() function. Let's execute the same file.

job = cron.new("/usr/bin/python3 -u /path/of/file/script.py")
Enter fullscreen mode Exit fullscreen mode

Now schedule the job.

job.minute.on(43)
job.hour.on(13)
job.day.on(25)
job.month.on(5) # That's 25th May 13:43
Enter fullscreen mode Exit fullscreen mode

Save the changes.

cron.write()
Enter fullscreen mode Exit fullscreen mode

Run this program and check crontab -l to ensure whether job is scheduled or not.

That's it! You have scheduled a job using Python.

Wait, hold on

This is my first article in dev.to. I was working on a project, then I came across job scheduling in Linux, which makes really easy to automate tasks. If you like this article, make sure you follow me on dev.to for more interesting articles.

Follow me on 👇🏻

Latest comments (0)