Friday 29 June 2018

Add Job to Cron (Crontab Command Examples) - Unix / Linux

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.

Unix Command, Linux Command, Crontab Command, LPI Study Material, LPI Certifications

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

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

Let see the usage of crontab command with examples.

1. List crontab entries


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

crontab -l
0 0 * * *  /usr/local/bin/list_unix_versions.sh

The above contab command displays the cron entries. Here the shell script for listing the unix versions (list_unix_version.sh) is scheduled to run daily at midnight.

2. 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

3. 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

4. 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 editing other user’s crontab entries:
crontab -u username -e

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

5. 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.

0 0 * * 0 /usr/local/bin/oracle_backup.sh

6. 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.

0 0,6,12,18 * * * /usr/bin/mysql_backup.sh

Here the list 0,6,12,18 indicates midnight, 6am, 12pm and 6pm respectively.

7. 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 sql server backup daily at midnight for the first 15 days in a month.

0 0 * 1-15 * /usr/bin/sql_server_backup.sh

8. Schedule job to run every minute.


The following crontab command runs the command to send emails to group of users for every minute.

* * * * * /bin/batch_email_send.sh

9. Taking backup of cron entries


Before editing the cron entries, it is good to take backup of the cron entries. So that even if you do mistake you can get back those entries from the backup.

crontab -l > /var/tmp/cron_backup.dat

10. Restoring the cron entries


You can restore the cron entries from the backup as

crontab cron_backup.dat

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 6 above.
◈ Hyphen (-): Indicates range of values. See example 7 above.

Disabling Emails:


By default the crontab sends emails to the local user if the commands or scripts produce any output. To disable sending of emails redirect the output of commands to /dev/null 2>&1.

0 0 * 20 * /usr/bin/online_backup.sh > /dev/null 2>&1

Note: you cannot schedule a job to run at second’s level as the minimum allowed scheduling is at minute level.

Related Posts

0 comments:

Post a Comment