Automation Using Cron Jobs in Linux

Umhayyaan
5 min readJan 28, 2024

--

Have you ever forgotten to take that backup and regretted it later on? Well technology is just so awesome, it does tons of tasks for us.Imagine being able to set something to run in the background , sparing you from the constant battle of remembering — super handy! That’s where cron jobs come in!

I got fascinated by cron jobs when we were doing Linux lessons at The Hacking School.

In this post, I’m going to provide you with an overview of cron jobs.

Let me start by explaining what a daemon is.

A daemon is a type of process that runs silently in the background and is regulated by events and conditions rather than direct user interaction. Daemons often start at system boot and continue running until the system is shut down.

Understanding Cron and Cron Jobs

Cron is a daemon. It is a time-based job-scheduler command-line utility on Unix-like operating Systems. It allows Linux and Unix users to schedule jobs to run periodically at specified intervals or time and date unattended.

The cron daemon runs in the background and regularly checks the system’s crontab files, which contain the schedule information for the jobs to be executed.

When a scheduled time or interval is reached, the cron daemon initiates the execution of the specified commands or scripts.

These automated tasks (scheduled commands or scripts) are known as Cron Jobs.

Cron operates based on scheduling instructions found in a crontab file.

The basic Crontab Syntax

Linux Crontab has six fields, each separated by a space. The first five fields indicate date and time of command execution. The sixth field specifies the absolute path of the command/script to be executed.

# * * * * * command-to-execute
# | | | | |
# | | | | |====> Day of week(0-7: 0-6 are Sunday to Saturday, 7 is also Sunday)
# | | | |
# | | | |======> Month (1-12)
# | | |
# | | |========> Day of month ( 1-31)
# | |
# | |==========> Hour (0-24)
# |
# |============> Minute (0-59)

In the month field numeric values from 1–12, where 1 is January, 2 is February and so on or three-character strings, based on name of the month like jan, feb, mar etc can be used.

Similarly, the fifth field, which represents day of week can either be written as numbers from 0–7 or as three-character strings based on the name of the day like mon, tue, wed etc.

In addition to these, certain symbols are also used.

Asterisk (*) → To select all possible values in a field. Example: Placing * in the month field to run the task every month.

Comma (,) → To separate multiple values in a field. Example: 15,45 in the minute field means the command will run at both the 15th and 45th minute of every hour.

Hyphen (-) → To set a range of values. Example: 3–7 in the day of month field will run the task from the 3rd to the 7th day of the month.

Forward slash (/) → To divide a value into steps. Forward slash is a convenient way to express repetitive patterns within a given time unit. Example: */5 in the minute field means the command will run every 5 minutes, i.e., at 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, and 55 minutes past the hour.

Now, let’s move on to the exciting part: scheduling it!

Setting up a Cron Job

Let’s schedule a script that writes “Hello there!” every two minutes every Sunday to a text file.

Step 1: Create the job that is to be automated- a .sh script file that prints “Hello there!”.

Lets create a folder called crons and create a hello.sh file inside it. (I have created the crons folder inside another folder called test.)

Let’s write the hello.sh code inside nano text editor:

Let’s test if hello.sh is working or not- let’s execute the commands inside script.sh using bash .

So, a file called hello.txt has been created and “Hello there!” has been added to it.

The first thing to do before scheduling a cron job is to check and change the permissions if required.

When individuals who are neither the user nor part of a designated group such as automated processes or third-party applications etc, attempt to perform an action, they, by default, only possess read permissions. Consequently, it becomes necessary to grant others execute permissions as well. Let’s change the permissions using chmod .

Now that hello.sh is working, and execute permissions have been changed, let’s schedule it to run every two minutes using a cron job. (Time to cron up!)

Step 2: Configuring the Cron Job

I love copy paste! So I’m going to pwd to get the absolute path of our hello.sh to copy paste it inside crontab.

Let’s create the crontab entry by crontab -e command.

crontab -e will open nano editor to enter the cron job. Let’s type in our crontab entry. First field represents every two minutes. Last field 0 is for Sunday.

After exiting, we can see the following.

Now let’s cat hello.txt file to check if our scheduled cron job is working or not. (Fingers crossed !)

Yay! So cool!

Now let’s crontab -r to delete our cron job.

Mission Accomplished!

What I’ve shown here is a simple example, but there are numerous real world use cases for employing cron. Cron jobs are essential tools for automating system maintenance, backups and other recurring activities.

For example, if you need to send email reminders every Friday night at midnight, cron jobs are your go-to. By scheduling specific commands through cron, you can automate the timely dispatch of reminders.

I hope you’ve enjoyed and learned from this. Until next time, happy scheduling!

--

--