job-scheduling

Linux crontab is similar to windows task schedules. Crontab are very useful for routine tasks like scheduling system scanning, daily backups etc. Crontab executes jobs automatically in back-end on specified time interval. For scheduling one time tasks you can use at command in Linux.

Linux crontab have six fields. 1-5 fields denotes time and 6’th fields are used for command/script

Field Description Allowed Value
MIN Minute field 0 to 59
HOUR Hour field 0 to 23
DOM Day of Month 1-31
MON Month field 1-12
DOW Day Of Week 0-6
CMD Command Any command to be executed.

Allowed special character (*, -, /, ?, #)

Asterik(*) – Match all values in the field or any possible value.
Hyphen(-) – To define range.
Slash (/) – 1st field /10 meaning every ten minute or increment of range.
Comma (,) – To separate items.
————————————————————————————————————————–

System administrator can use predefine cron directory as shown below.

/etc/cron.d
/etc/cron.daily
/etc/cron.hourly
/etc/cron.monthly
/etc/cron.weekly
————————————————————————————————————————–

[rafi@openstack ~]$ touch file{1..25} /home/rafi/findcommand/

[rafi@openstack ~]$ ls -l /home/rafi/findcommand/

total 0

-rw-rw-r– 1 rafi rafi 0 Apr 14 21:25 file1

-rw-rw-r– 1 rafi rafi 0 Apr 14 21:25 file10

-rw-rw-r– 1 rafi rafi 0 Apr 14 21:25 file11

-rw-rw-r– 1 rafi rafi 0 Apr 14 21:25 file12

-rw-rw-r– 1 rafi rafi 0 Apr 14 21:25 file13

-rw-rw-r– 1 rafi rafi 0 Apr 14 21:25 file14

-rw-rw-r– 1 rafi rafi 0 Apr 14 21:25 file15

-rw-rw-r– 1 rafi rafi 0 Apr 14 21:25 file16

-rw-rw-r– 1 rafi rafi 0 Apr 14 21:25 file17

-rw-rw-r– 1 rafi rafi 0 Apr 14 21:25 file18

-rw-rw-r– 1 rafi rafi 0 Apr 14 21:25 file19

-rw-rw-r– 1 rafi rafi 0 Apr 14 21:25 file2

-rw-rw-r– 1 rafi rafi 0 Apr 14 21:25 file20

-rw-rw-r– 1 rafi rafi 0 Apr 14 21:25 file21

-rw-rw-r– 1 rafi rafi 0 Apr 14 21:25 file22

-rw-rw-r– 1 rafi rafi 0 Apr 14 21:25 file23

-rw-rw-r– 1 rafi rafi 0 Apr 14 21:25 file24

-rw-rw-r– 1 rafi rafi 0 Apr 14 21:25 file25

-rw-rw-r– 1 rafi rafi 0 Apr 14 21:25 file3

-rw-rw-r– 1 rafi rafi 0 Apr 14 21:25 file4

-rw-rw-r– 1 rafi rafi 0 Apr 14 21:25 file5

-rw-rw-r– 1 rafi rafi 0 Apr 14 21:25 file6

-rw-rw-r– 1 rafi rafi 0 Apr 14 21:25 file7

-rw-rw-r– 1 rafi rafi 0 Apr 14 21:25 file8

-rw-rw-r– 1 rafi rafi 0 Apr 14 21:25 file9

[rafi@openstack ~]$ date

Thu Apr 14 21:47:05 MST 2016

[rafi@openstack ~]$ crontab -e

* * * * * find /home/rafi/findcommand/ -type f -mmin +5  -exec rm -f {} \;

[rafi@openstack ~]$ ls -l /home/rafi/findcommand/

total 0

————————————————————————————————————————–

  1. Scheduling a Job For a Specific Time

The basic usage of cron is to execute a job in a specific time as shown below. This will execute the Full backup shell script (full-backup) on 10th June 08:30 AM.

Please note that the time field uses 24 hours format. So, for 8 AM use 8, and for 8 PM use 20.

30 08 10 06 * /home/mohammed/full-backup

30 – 30th Minute

08 – 08 AM

10 – 10th Day

06 – 6th Month (June)

* – Every day of the week

————————————————————————————————————————–

  1. Schedule a Job For More Than One Instance (e.g. Twice a Day)

The following script take a incremental backup twice a day every day.

This example executes the specified incremental backup shell script (incremental-backup) at 11:00 and 16:00 on every day. The comma separated value in a field specifies that the command needs to be executed in all the mentioned time.

00 11,16 * * * /home/mohammed/incremental-backup

00 – 0th Minute (Top of the hour)

11,16 – 11 AM and 4 PM

* – Every day

* – Every month

* – Every day of the week

————————————————————————————————————————–

  1. Schedule a Job for Specific Range of Time (e.g. Only on Weekdays)

If you wanted a job to be scheduled for every hour with in a specific range of time then use the following.

Cron Job everyday during working hours

This example checks the status of the database everyday (including weekends) during the working hours 9 a.m – 6 p.m

00 09-18 * * * /home/mohammed/check-db-status

00 – 0th Minute (Top of the hour)

09-18 – 9 am, 10 am,11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm

* – Every day

* – Every month

* – Every day of the week

Cron Job every weekday during working hours

This example checks the status of the database every weekday (i.e excluding Sat and Sun) during the working hours 9 a.m – 6 p.m.

00 09-18 * * 1-5 /home/mohammed/check-db-status

00 – 0th Minute (Top of the hour)

09-18 – 9 am, 10 am,11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm

* – Every day

* – Every month

1-5 -Mon, Tue, Wed, Thu and Fri (Every Weekday)

————————————————————————————————————————–

  1. How to View Crontab Entries?
    crontab -l

crontab -u shaik -l

————————————————————————————————————————-

  1. How to Edit Crontab Entries?

crontab -e

@yearly /home/mohammed/sample

crontab -u shaik -e

00 09-18 * * * /home/mohammed/check-db-status

————————————————————————————————————————-

  1. Schedule a Job for Every Minute Using Cron.

Ideally you may not have a requirement to schedule a job every minute. But understanding this example will will help you understand the other examples mentioned below in this article.

* * * * * CMD

The * means all the possible unit — i.e every minute of every hour through out the year. More than using this * directly, you will find it very useful in the following cases.

When you specify */5 in minute field means every 5 minutes.

When you specify 0-10/2 in minute field mean every 2 minutes in the first 10 minute.

Thus the above convention can be used for all the other 4 fields.

————————————————————————————————————————–

  1. Schedule a Background Cron Job For Every 10 Minutes.

Use the following, if you want to check the disk space every 10 minutes.

*/10 * * * * /home/ramesh/check-disk-space

It executes the specified command check-disk-space every 10 minutes through out the year. But you may have a requirement of executing the command only during office hours or vice versa. The above examples shows how to do those things.

Instead of specifying values in the 5 fields, we can specify it using a single keyword as mentioned below.

There are special cases in which instead of the above 5 fields you can use @ followed by a keyword — such as reboot, midnight, yearly, hourly.

Table: Cron special keywords and its meaning

Keyword Equivalent
@yearly 0 0 1 1 *
@daily 0 0 * * *
@hourly 0 * * * *
@reboot Run at startup.

@yearly /home/mohammed/myscript

@monthly /home/mohammed/myscript

@weekly /home/mohammed/myscript

@daily /home/mohammed/myscript

@hourly /home/mohammed/myscript

@monthly /home/mohammed/myscript

————————————————————————————————————————-

  1.  Schedule a cron to execute on every Sunday at 5 PM.

This type of cron are useful for doing weekly tasks, like log rotation etc.

0 17 * * sun  /scripts/script.sh

————————————————————————————————————————–

  1.  Schedule a cron to execute on selected months.

Some times we required to schedule a task to be executed for selected months only. Below example script will run on January, May and August months.

* * * jan,may,aug *  /script/script.sh

————————————————————————————————————————–

  1. Schedule a cron to execute on every four hours.

If you want to run script on 4 hours interval. It can be configured like below.

0 */4 * * * /scripts/script.sh

————————————————————————————————————————–

  1. Schedule a cron to execute twice on every Sunday and Monday.

To schedule a task to execute twice on Sunday and Monday only. Use following settings to do it.

0 4,17 * * sun,mon /scripts/script.sh

————————————————————————————————————————–

  1. Schedule a cron to execute on every 30 Seconds.

To schedule a task to execute on every 30 seconds is not possible by time parameters, But it can be done by schedule same cron twice like below.

* * * * * /scripts/script.sh

* * * * *  sleep 30; /scripts/script.sh

————————————————————————————————————————–
13. Remove Crontab Entry

Caution: Crontab with -r parameter will remove complete scheduled jobs without confirmation from crontab. Use -i option before deleting user’s crontab.

# crontab -r
————————————————————————————————————————–
14. Prompt Before Deleting Crontab

crontab with -i option will prompt you confirmation from user before deleting user’s crontab.

# crontab -i -r
crontab: really delete root’s crontab?
————————————————————————————————————————–

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s