Automate Repetitive (Boring) Tasks With Cron Jobs ⚙
Tags: linux, tutorial, productivity, opensource
Published: 2022-05-21 16:12:33

## What is Crontab?
Cron is a utility program that lets users automate certain tasks to be executed on a timely basis. The tasks scheduled in cron are called cron jobs. Scheduling cron jobs lets users automate tasks at a specified time or at every instance of the specified time on a virtual private server or UNIX-like operating systems. Crontab (cron) stands for cron table, it is a file that contains a list of the all the cron jobs.

## Use Cases
I could give a 1000 use cases. But, right of the top of my mind, this could be a lifesaver for some people like sysadmins and webdevs. sysadmins do a lot of repetitive tasks on a daily basis like moving a file from one folder to another or running a specific job. And as for the webdevs, they might have to take a backup of their websites on daily basis or even on a hourly basis. Cron jobs could come in handy at situations like these.

## Why Crontab?
There are some pretty good alternatives for crontabs out there. The famous opensource task scheduler, [Apache airflow](https://airflow.apache.org/) was built taking inspiration from crontab. The advent of tools like airflow into the task automation arena while crontabs were still present was because of the following reasons:
* The shortest interval between jobs is 60 seconds. With cron, you won’t be able to repeat a job every 59 seconds or less.
* Centralized on one computer. Cron jobs can’t be distributed to multiple computers on a network. So if the computer running cron crashes, the scheduled tasks won’t be executed, and the missed jobs will only be able to be run manually.
* No auto-retry mechanism. Cron is designed to run at strictly specified times. If a task fails, it won’t run again until the next scheduled time. This makes cron unsuitable for incremental tasks. 

Yes, crontabs are pretty much the story of yesterday. And you thought I would convince you to use crontab right? Well, I'm about to. Crontabs are still great for simple task scheduling that don't have to be run every second or so. Scheduling tools like airflow are for more complex tasks. Crontab is easy to learn and it just gets our simple task at hand done right on time without much hassle. Now lets play a bit with crontab.

--------

## Getting started with crontab
The MacOS comes with crontab pre-installed. run `crontab -l` in your terminal to check. Now lets try to schedule a simple daily task.

### Task at hand
I have a python script named `news.py` that does one simple thing, It opens news.google.com with the for you section upfront. I like reading news every morning, that too news curated and aligned to my taste. Well, who knows you better than google? 😉 Hence news.google.com

Now the file that we talked about, `news.py` has only two lines. This when executed opens the google news webpage in my default browser, pretty straightforward. Now if you want to take a look at the file here you go:

```python
import webbrowser
webbrowser.open_new(url='https://news.google.com/foryou?hl=en-IN&gl=IN&ceid=IN%3Aen')
```
Lets say everyday I want this to be open exactly at 09:00 AM in the morning right after I wake up and take a look at my mails in my laptop. We are going to schedule this cron.

### Scheduling with crontab
Now that we have a pretty clear picture of what our task is, lets dive in and get our task scheduled with crontab.

To schedule this task, just run
```
crontab -e
```
This will open a new cron file in cron's default underlying editor vi and I would strongly recommend that you not use a standard editor (such as Vi, Vim, Emacs, Nano, or any of the many other editors that are available). Using the crontab command not only allows you to edit the command, it also restarts the crond daemon when you save and exit the editor. 

So instead you can run, something like
```
EDITOR='code' crontab -e
```
This will open the cron file in VScode, my favourite editor. You can use your editor of choice.

#### Schedule syntax
The schedule syntax of crontabs are pretty simple. The time syntax for crontab looks something like this * * * * *. Each of the asterisks represent each of the time component. Now if we visualize this:

```
* * * * *  command to execute
│ │ │ │ │
│ │ │ │ └─── day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
│ │ │ └──────── month (1 - 12)
│ │ └───────────── day of month (1 - 31)
│ └────────────────── hour (0 - 23)
└─────────────────────── min (0 - 59)
```
The scheduling component followed by the command to be executed is the syntax for crontabs. If you are having a hard time setting up the correct time component, There are some websites that could help you like [crontab.guru](crontab.guru). My colleague introduced me to this website when I first started with crontabs.

Now that we know the syntax of crontabs and since we have opened our cron file in our editor, lets go on and schedule it. 

In the empty cron file that we just opened, paste the below and save it.

```
0 9 * * * cd /Users/adheeban.m/Documents/work/test/ && python3 news.py
```

Since I want our task to be executed everyday at 9AM, the time component `0 9 * * *` is being followed by the commands to be executed at that time.

Congrats, you scheduled your first ever crontab. This will open up my favourites curated news feed daily at 9AM, just right before I wake up and check my mails in my laptop. To check that If you have successfully scheduled your task. Run `crontab -l` to check the all the scheduled crontabs. 

This is just one use case of crontabs. The apllications are endless. Keep exploring and I hope you learned something new from this post. See you in the next one. Cheers!