How would I get a cron job to run every 30 minutes on Linux?


In order to create a crontab job to run every 30 minutes we first need to explore and understand what a crontab job is.

A crontab is nothing but a list of commands that we can run during a cron job. A cron job is a utility that schedules automatic execution of commands at specific times.

We can start a cron job with the help of bash script by following the commands shown below −

crontab -e

This will open a file which you can edit, insert the cron job shell script in the above file and then close that file.

Just insert the code shown below in the above file

* * * * * sample.sh

The above command contains 5 *, where each * indicates the time and then follows the stars. We have the script which we want to run as a cron job. In the sample.sh we need to write the following command to make the environment variables available to it.

Now we understand how we can create a crontab job, it is time to understand what these * actually mean and how we can replace their values to solve our particular question.

The five * in the above command actually has a separate meaning attached to them.

These mainly mean −

Seconds: 0-59

Minutes: 0-59

Hours: 0-23

Day of Month: 1-31

Months: 0-11

Day of Week: 0-6

Now that we know what the cron job actually means and how it works, it’s time that we understand how we can make a cron job run every 30 minutes.

While there are different variations possible for the same result, the most basic command that will make sure that a job runs when the minute of each hour is 0 or 30 is shown below

Command

0,30 * * * * sample.sh

The above command will make sure that the sample.sh bash script will run when the minute of each hour is 0 or 30 (like it would run at 2:00, 2:30, 3:00). 

Updated on: 31-Jul-2021

756 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements