Sunday 7 September 2014

Linux Job Scheduling or Crontab


UNIX or Linux operating system provides a feature for scheduling the jobs. You can
setup command or scripts which will run periodically at the specified time. The
crontab is command used to add or remove jobs from the cron. The cron service
is a daemon runs in the background and checks for /etc/crontab file, /etc/con.*/
directories and /var/spool/cron/ directory for any scheduled jobs.
Each user has a separate /var/spool/cron/crontab file. Users are not allowed
directly to modify the files. The crontab command is used for setting up the jobs
in the cron.

The format of crontab command is
* * * * * command to be executed

You can easily remember this command in the below format:
MI HH DOM MON DOW command to be executed
The field descriptions of the crontab are explained below:
MI : Minutes from 0 to 59
HH : Hours from 0 to 23
DOM : Day of month from 0 to 31
MON : Months from 1 to 12
DOW : Day of week from 0 to 7 (0 or 7 represents Sunday)
Command: Any command or script to be scheduled

Understanding the Operators:

There are three operators allowed for specifying the scheduling times. They are:
 Asterisk (*): Indicates all possible values for a field. An asterisk in the
     month field indicates all possible months (January to December).
 Comma (,): Indicates list of values. See example 2 below.
 Hyphen (-): Indicates range of values. See example 3 below.

List crontab entries
You can list out all the jobs which are already scheduled in cron. Use "crontab -l"
for listing the jobs.

[root@rhel6 ~]# crontab –l
0 0 * * * /cslit/backup/backup.sh

The above contab command displays the cron entries. Here the shell script for
listing the backup is scheduled to run daily at midnight.

List crontab entries of other users
To list the corntab entries of other user in the UNIX, use the -u option with
crontab. The syntax is shown below:

crontab -u username -l
[root@rhel6 ~]# crontab -u nzrul -l

Removing all crontab entries
 You can un-schedule all the jobs by removing them from the crontab. The syntax
 for removing all the crontab entries is
crontab -r

For removing other user’s crontab entries:
crontab -u username –r
[root@rhel6 ~]# crontab -r
[root@rhel6 ~]# crontab -u nazrul -r

Editing the crontab
You can edit the crontab and add a new job to it. You can also remove an existing
job from the crontab. Use the -e option for editing the crontab.
crontab -e

For removing other user’s crontab entries:
crontab -u username –e
[root@rhel6 ~]# crontab -e
[root@rhel6 ~]# crontab -u nzrul -e

This will open a file in vim editor. Now use the vi commands for adding, removing
the jobs and for saving the crontab entries.

Example 01: Schedule a job to take oracle backup on every Sunday at midnight.
Edit crontab using "crontab -e" and append the following entry in the file.
[root@rhel6 ~]# crontab –e
0 0 * * 0 /cslit/backup/oracle_backup.sh

Example 02: Schedule a job to run every six hours in a day
You can schedule a job to run more than once in a day. As an example the
following crontab entry takes the mysql backup more than once in a day.
[root@rhel6 ~]# crontab –e
0 0,6,12,18 * * * /cslit/backup/mysql_backup.sh
Here the list 0, 6, 12, 18 indicates midnight, 6am, 12pm and 6pm respectively.

Example 03: Schedule job to run for the first 15 days of the month.
You can schedule a job by specifying the range of values for a field. The following
example takes the file server backup daily at midnight for the first 15 days in a
month.
[root@rhel6 ~]# crontab –e
0 0 * 1-15 * /cslit/backup/fileserver_backup.sh

Example 04: Schedule job to run every five minute.
The following crontab command runs the command to website access reports of
users for every five minute.
[root@rhel6 ~]# crontab –e
*/5 * * * * /cslit/report/website_access.sh

Example 05: Run command at 7:00am each weekday [Monday – Friday]
[root@rhel6 ~]# crontab –e
00 07 * * 1-5 /cslit/report/server_log.sh

Example 06: Run command at 8:00am, 10:00am and 2:00pm every day
[root@rhel6 ~]# crontab –e
00 08,10,14 * * * /cslit/report/monitoring.sh

Example 07: Schedule job to run every minute.
[root@rhel6 ~]# crontab –e
* * * * * /cslit/report/diskstatus.sh

Example 08: Run a crontab command every hour
To run a Linux or UNIX crontab every hour of every day, you use very similar
crontab syntax. Here's a crontab entry I use squid cache status every five
minutes after every hour:
[root@rhel6 ~]# crontab –e
5 * * * * `/cslit/report/squid_cache.sh`

Example 09: Run a crontab entry every day
Here's a crontab example that shows how to run a command from the cron daemon
once every day. In this command I run my backup scripts at 4:30 p.m. every day:
[root@rhel6 ~]# crontab –e
30 16 * * * `/cslit/backup/daily_backup.sh`

Example 10: To execute a cronjob at 4:40 am every 1st of the month
[root@rhel6 ~]# crontab –e
40 13 15 * * /cslit/backup/monthly_backup.sh

Example 11: More crond command in without script
41 16 * * * `cp -rv /var/ftp/pub/Server/z* /backup`
* * * * * `cp /etc/passwd /backup`
54 16 * * * /backup/mybackup.sh
59 23 * * * /sbin/shutdown -h now
                                                                 
                                                                   *** THE End***

1 comment:

  1. Nice post. I was checking constantly this blog and I’m impressed! Extremely useful info specially the last part I care for such information a lot. I was seeking this certain info for a long time. Thank you and good luck.scheduling air conditioning repairs

    ReplyDelete

How to install clamAV on Centos 6

  Install EPEL repo: Before we can do proceed, you must ensure that you have the EPEL yum repository enabled. To do this, CentO...