Cron Expressions To Run Command At Predefined Interval

Linux Crontab allows you to run commands, apps, or scripts at a predefined interval. You can schedule the run to occur at the desired interval. Crontab runs in the background as a daemon and checks the crontab files in the /etc/crontab and /etc/cron.*/ directories. These include cron.d/, cron.daily/, cron.hourly/, cron.monthly/, cron.weekly/ directories. In this article, we’ll go over cron expressions and how to use Linux Crontab to schedule your commands to run daily, weekly, or every minute, depending on your needs.

How To Use Crontab?

In Linux, every user has its own cron file which can be edited by using crontab command.

The section following this explains various fields for cron expression. Cron is pre-bundled in Linux and no need to separately install it. To open crontab file use command – Open crontab file

crontab -e
open cron file

​Linux Crontab Expressions

Let’s understand how we can use cron expressions to configure commands to run at a specific time interval. It might look a little difficult but believe me it’s easy.

The cron expressions are mainly divided into two parts :

  1. Date part: that defines at what interval or time the command has to be executed.
  2. Command: command to execute.

The expression takes the form – Cron expressions

min hour day month dayOfWeek Command
min : Minute (0-59)
hour : Hour (0-23)
day : Day of month (1-31)  
month : Month (1-12)
dayOfWeek : Day of week (0 - 7) [Sunday = 0 or 7]
Command: command to run as cron job.

Cron Operators

Cron operators add flexibility in defining command timings.

* : Asterisk (*) is for every possible value, for example, * in minute would mean every minute ​Example (Run everyday at 5 am)

0 5 * * 1 sh /home/savan.patel/backup.sh

,  : Comma specifies the list of values. Example (Run everyday at 5 am, 7 am and 10 am)

0 5,7,10 * * * sh /home/savan.patel/backup.sh

: dash specifies range of values. Example (Run at 5,6,7 am daily)

0 5-7 * * 1 sh /home/savan.patel/backup.sh

/ :  Seperater​

This is step operator use to specify for terms like every hour. For example to run command at every 3 hours you would use */3 in your expression Example Run every 3 hours

* */3 * * * sh /home/savan.patel/backup.sh

Cron Expressions Usage

Let’s take some more examples to understand the usage of cron expressions. Run backup script every 5 mins

*/5 * * * * sh /home/savan.patel/backup.sh

Run backup script every 5 hours

* */5 * * * sh /home/savan.patel/backup.sh

Run backup script every sunday at midnight

0 0 * * 7 sh /home/savan.patel/backup.sh

Special Crontab Strings

Besides operators, there are special strings defined by cron that are very easy to use. Special Crontab strings

@reboot : run once at startup
@yearly : run yearly on 1st Jan midnight
@annually : same as yearly
@monthly : run on 1st of month every month midnight
@weekly : run once a week
@daily : run daily at midnight
@midnight : same as @daily

Syntax To Use Special Strings

@STRING Command ​Run the backup script weekly

@weekly sh  /home/savan.patel/backup.sh

​Run the backup script daily

@daily sh /home/savan.patel/backup.sh

Viewing Existing Cron Configurations

To list out user’s existing cron configurations you can use command – List existing cron

crontab -l

Generating Linux Crontab Expressions With Tool ​

Additionally, if you are still confused with Linux cron expressions and it’s usage, there are online tools available that help you to build the cron expressions easily.

CronMaker: http://www.cronmaker.com/
Crontab generator: http://crontab-generator.org/

SHARE THIS POST

MassiveGRID Banner

Leave a Reply

Your email address will not be published. Required fields are marked *