Keep walking

Whatever you need to grow, just do it faster…

Add Job to Cron – Unix / Linux Tutorials

Posted by ZyK on 25/08/2012

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

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.

The crontab command has the built in constant strings for specifying the scheduling times. They are:

@reboot : Run job once after booting the unix or linux system.
@yearly : Run the job once in a year
@annually : Run once in a year
@monthly : Run once in a month
@weekly : Run once in a week
@daily : Run once in a day
@midnight : Run once in a day at midnight
@hourly : Run for every hour

Example:

@hourly /usr/local/bin/automate_script

Src: http://www.folkstalk.com/

About these ads

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

 
Tự tin cùng Tiếng Anh

Non-stop learning and say you will

Keep walking

Whatever you need to grow, just do it faster...

A Patchwork Life

writing, teaching, and piecing it all together

Class Teaching

Finding & sharing teaching 'bright spots'

SCP's Kitchen

Love to cook and cook to be loved

Bếp Rùa ♥ Hơi ấm gia đình tỏa ra từ gian bếp nhỏ

Luôn luôn đỏ lửa ♥ Nồng ấm yêu thương

NẤU VÀ ĂN

Những công thức đơn giản tặng anh trai đi du học

discovering the world's diversity

Jana & Alex travelling by bike

zanydibuiblog

A great WordPress.com site

VLXD Tổng hợp Khánh Linh Lào Cai

Chuyên Kinh doanh VLXD, Thiết bị Cấp thoát nước,gia công cơ khí, thiết kế và lắp đặt nhà xưởng,...

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: