Linux Job Scheduling using Python
Tags: python, linux, programming
Published: 2022-05-25 09:29:38

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

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

```
30 18 25 5 *
```

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

```python
from tkinter import *

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

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

window.mainloop()
```

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

Open terminal and type

```
crontab -e
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/10jwxm236i6rjxeth6gz.png)


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

<b>Note: </b> 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k90t6miakcl4vi5d01eu.png)

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

## 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
```

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

```python
from crontab import CronTab
cron = CronTab(user="<username>")
```

<b>Note: </b>Type `whoami` in terminal to get the username.

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

```python
job = cron.new("/usr/bin/python3 -u /path/of/file/script.py")
```

Now schedule the job.

```python
job.minute.on(43)
job.hour.on(13)
job.day.on(25)
job.month.on(5) # That's 25th May 13:43
```

Save the changes.

```python
cron.write()
```

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](https://github.com/subhendudash02/pyagenda), 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 👇🏻
 - [GitHub](https://github.com/subhendudash02)
 - [Twitter](https://twitter.com/SubhenduDash02)
 - [LinkedIn](https://www.linkedin.com/in/subhendu21/)