Crontab – Cron Job 20 Examples To Schedule Jobs In Linux

Crontab reduces server administrator work and executes the scheduled job at the given time without fail. Each user has their own crontab, and commands in any given crontab will be executed as the user who owns the crontab. I have give more than ten practical examples below for better understanding. You can have a look into it.

1) Install & print crontab entry

Use the below command to install & view the list of crons scheduled.

# How to install cron in CentOS/RHEL/Fedora #
[email protected] [~]# yum install cronie

# How to list crontab entry #
[email protected] [~]# crontab -l

# How to edit crontab entry #
[email protected] [~]# crontab -e

# How to list particular user crontab entry #
[email protected] [~]# crontab -u  -l

# How to edit particular user crontab entry #
[email protected] [~]# crontab -u  -e

# How to get more details about crontab #
[email protected] [~]# man crontab

# How to remove cron entry #
[email protected] [~]# crontab -r

2) crontab format

The crontab file has six fields like minute, hour, day of month, month, day of week and command to be executed. First 5 (1-5) holding time stamp and last (6th field) holding command to be executed.

# Crontab syntax/format #

 .---------------- minute (0 - 59)
 |  .------------- hour (0 - 23)
 |  |  .---------- day of month (1 - 31)
 |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...dec
 |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
 |  |  |  |  |  .- user-name command to be executed
 |  |  |  |  |  |
 *  *  *  *  *  *

3) crontab details

You must know about allowed crontab special character’s, Predefine cron directory and cron configuration file to move forward

# Allowed crontab special character's #
(*) -> Match all values in the field or any possible value.
(-) -> To define range.
(/) -> 1st field /10 meaning every ten minute or increment of range.
(,) -> To separate items.

# Predefine cron directory #
/etc/cron.d
/etc/cron.daily
/etc/cron.hourly
/etc/cron.monthly
/etc/cron.weekly
/etc/cron.deny

# crontab config file #
[email protected] [~]# cat /etc/crontab

4) How to schedule a cron once in a day

This example will be execute the scheduled job of mysql backup shell script at 6.0 pm every day.

# schedule a cron daily basics #
0 18 * * * /bin/mysql_backup.sh

# Details #
0 --> 0'th Minute
18 --> 6.0 PM
* --> Every day
* --> Every month
* --> Every day of the week

5) How to schedule a cron twice in a day

This example will be execute the scheduled job of mysql backup shell script twice in a day 6.0 am & 6.0 pm.

# schedule a cron twice in a day #
0 6,18 * * * /bin/mysql_backup.sh

# Details #
0 --> 0'th Minute
6,18 --> 6.0 AM & 6.0 PM
* --> Every day
* --> Every month
* --> Every day of the week

6) How to schedule a cron between time in a day

This example will be execute the scheduled job of mysql backup shell script between 8.0 am to 12.0 pm. So, totally 5 times running.

# schedule a cron between time in a day #
0 8-12 * * * /bin/mysql_backup.sh

# Details #
0 --> 0'th Minute
8-12 --> 8am,9am,10am,11am & 12pm
* --> Every day
* --> Every month
* --> Every day of the week

7) How to schedule a cron every minute

This example will be execute the scheduled job of cleat tmp shell script every minute.

# schedule a cron every minute #
* * * * * /bin/clear-tmp.sh

# Details #
* --> Every Minute
* --> Every hour
* --> Every day
* --> Every month
* --> Every day of the week

8) How to schedule a cron every five minute

This example will be execute the scheduled job of clear tmp shell script every five minute.

# schedule a cron every 5 minute #
*/5 * * * * /bin/clear-tmp.sh

# Details #
* --> Every 5 Minute
* --> Every hour
* --> Every day
* --> Every month
* --> Every day of the week

9) How to schedule a cron every 2 hours

This example will be execute the scheduled job of clear tmp shell script every two hours.

# schedule a cron every 5 minute #
* */2 * * * /bin/clear-tmp.sh

# Details #
* --> Every Minute
*/2 --> Every hour
* --> Every day
* --> Every month
* --> Every day of the week

10) How to schedule a cron once in a week

This example will be execute the scheduled job of website backup shell script every Saturday at 6 pm.

# schedule a cron once in a week #
0 18 * * 6 /bin/website_backup.sh

# Details #
0 --> 0'th Minute
18 --> 6 pm
* --> Every day
* --> Every month
6 --> Every week Saturday

11) How to schedule a cron twice in a week

This example will be execute the scheduled job of website backup shell script every Sunday at 6 am & 6 pm.

# schedule a cron once in a week #
0 18 * * sun /bin/website_backup.sh

# Details #
0 --> 0'th Minute
6,18 --> 6 am & 6 pm
* --> Every day
* --> Every month
sun --> Every week Sunday

12) How to schedule a cron in selected days

This example will be execute the scheduled job of website backup shell script every week Tuesday & Friday at 6 pm.

# schedule a cron once in a week #
0 18 * * tue,fri /bin/website_backup.sh

# Details #
0 --> 0'th Minute
18 --> 6 pm
* --> Every day
* --> Every month
tue,fri --> Every week tuesday & friday

13) How to schedule a cron in selected months

This example will be execute the scheduled job of website backup shell script on March, July & December at 6 pm.

# schedule a cron once in a week #
0 18 * mar,jul,dec * /bin/website_backup.sh

# Details #
0 --> 0'th Minute
18 --> 6 pm
* --> Every day
mar,jul,dec --> March, July & December
* --> > Every day of the week

14) How to schedule multiple task in single cron

This example will be execute the scheduled job of website backup & clear tmp shell script on every two hours.

# schedule a cron once in a week #
0 */2 * * * /bin/website_backup.sh; /bin/clear-tmp.sh
or
0 */2 * * * /bin/website_backup.sh && /bin/clear-tmp.sh

# Details #
0 --> 0'th Minute
*/2 --> Every two hours
* --> Every day
* --> Every month
* --> > Every day of the week

15) Cron special string and meaning’s

We can use @string instead of long commands like (*****), see below available strings and effective examples.

string Value meaning
@reboot Runs at boot
@hourly 0 * * * * Runs once an hour
@daily 0 0 * * * Runs once a day
@midnight 0 0 * * * Runs once a day
@weekly 0 0 * * 0 Runs once a week
@monthly 0 0 1 * * Runs once a month
@yearly 0 0 1 1 * Runs once a year
@annually 0 0 1 1 * Runs once a year

16) Schedule a cron using @hourly string

@hourly string is similar to (0 * * * *). This example will be execute the scheduled job of website backup shell script on first minute of every hour.

# schedule a cron using @hourly string #
@hourly /bin/website_backup.sh

# Details #
0 --> 0'th Minute
* --> Every hour
* --> Every day
* --> Every month
* --> > Every day of the week

17) Schedule a cron using @daily string

@daily string is similar to (0 0 * * *). This example will be execute the scheduled job of website backup shell script on first minute & first hour of every day.

# schedule a cron using @daily string #
@daily /bin/website_backup.sh

# Details #
0 --> 0'th Minute
0 --> 1'th hour
* --> Every day
* --> Every month
* --> > Every day of the week

18) Schedule a cron using @midnight string

@daily string is similar to (0 0 * * *). This example will be execute the scheduled job of website backup shell script on first minute & first hour of midnight every day.

# schedule a cron using @midnight string #
@midnight /bin/website_backup.sh

# Details #
0 --> 0'th Minute
0 --> 0'th hour
* --> Every day
* --> Every month
* --> Every day of the week

19) Schedule a cron using @weekly string

@weekly string is similar to (0 0 * * 0). This example will be execute the scheduled job of website backup shell script on first minute & first hour of Sunday every week.

# schedule a cron using @weekly string #
@daily /bin/website_backup.sh

# Details #
0 --> 0'th Minute
0 --> 1'th hour
* --> Every day
* --> Every month
0 --> Every Sunday

20) Schedule a cron using @monthly string

@monthly string is similar to (0 0 1 * *). This example will be execute the scheduled job of website backup shell script on first minute & first hour of every month.

# schedule a cron using @monthly string #
@daily /bin/website_backup.sh

# Details #
0 --> 0'th Minute
0 --> 1'th hour
1 --> 1'st day of month
* --> Every month
* --> Every day of the week

21) Schedule a cron using @yearly string

@yearly string is similar to (0 0 1 1 *). This example will be execute the scheduled job of yearly maintenance shell script on first minute, first hour, 1’st day of month & first month (January) of every year.

# schedule a cron using @yearly string #
@daily /bin/yearly-maintenance.sh

# Details #
0 --> 0'th Minute
0 --> 1'th hour
1 --> 1'st day of month
1 --> Once in a year
* --> Every day of the week

22) Schedule a cron using @annually string

@annually string is similar to (0 0 1 1 *). This example will be execute the scheduled job of yearly maintenance shell script on first minute, first hour, 1’st day of month & first month (January) of every year.

# schedule a cron using @annually string #
@daily /bin/yearly-maintenance.sh

# Details #
0 --> 0'th Minute
0 --> 1'th hour
1 --> 1'st day of month
1 --> Once in a year
* --> Every day of the week

23) How to disable email notifications

By default a cron job will send an email alert to the user. If you like to disable, just add the below line to end of the command.

# disable email notifications for cronjob #
/dev/null 2>&1

# Example : This job wont trigger mail to user after executing #
*/5 * * * * /bin/clear-tmp.sh >/dev/null 2>&1

Hope this article was useful for you. Kindly provide your valuable feedback/comments in the commenting section. Stay tuned with us !!

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...