Saturday, 19 April 2014

Schedule Your Jobs in Linux With CRON


    Most of the Linux users are aware of how commands are run, processes are manipulated and scripts are executed in terminal. But, if you are a Linux system administrator, you might want them to start and execute automatically in the background. As an example, you might consider running a backup job every day, at a specific time, automatically. Or you might consider an example of collecting inventory data of the systems deployed across your network, by running a script automatically on monthly basis. But, how to schedule these jobs and execute them automatically in Linux?

    There is an utility in Linux known as CRON with which you can start your jobs automatically at a desired time and schedule them to get executed periodically.

    Cron utility consists of two parts: The cron daemon and the cron configuration files. Cron daemon is just like any service that is started automatically whenever your system boots. Cron configuration files hold the information of what to do and when to do. The main job of cron daemon is to inspect the configuration regularly (every minute to be more precise) and check if there is any job to be completed.

    In the /etc directory, you will find some sub-directories namely cron.hourly, cron.daily, cron.weekly and cron.monthly. You can put your scripts in these directories, and as their names suggest, they will be automatically executed after certain period of time. For example, if you wish to run a job or service regularly after every week, simple put the script in /etc/cron.weekly directory.

In case you need to execute a script after every fortnight, keeping the script in any of the above mentioned directories will not help. But, every problem has a solution. You can launch crontab (CRON TABle) editor which provides an interface that is very much similar to vi editor using the command crontab –e. This will start the crontab editor to create cron job for the root user. If you are root and you wish to create cron job for other user, you can use command like crontab -u [USER] -e.

The general format for creating a cron job is as follows:

[MIN] [HR] [DOM] [MON] [DOW] [COMMAND] 
MIN : It is the Minute Field which can take values from 0 to 59.

HR : It is the HOUR Field which can take values from 0 to 23.

DOM : It is the Day of Month Field which can take values from 1 to 31.

MON : It is the Month Field which can take values from 1 to 12.

DOW : It is the Day of Week Field which can take values from 0 to 7, where 0 and 7 are Sundays.

Apart from this, there are some keywords which might help you in creating cron jobs without dealing with the numbers.

@hourly : It is equivalent to running a job at every 0th minute of each hour.

@daily : It is equivalent to running a job daily at 00:00 Hrs.

@yearly : It is equivalent to running a job at 00:00 Hrs on 1st Jan of every year.

@reboot : Well, this is self-explanatory.

Now, let’s start making our own cron jobs. Here we go!

1. Editing Cron Table (Crontab) Entries

As mentioned earlier, crontab editor is almost the same as the VI editor. If you are familiar with the VI editor, you can manipulate crontab editor very smoothly. To start editing the crontab entries, launch the crontab editor using following command:

crontab –e
You can add or modify the entries here and once you’re done, press :wq which will write the changes and save the file.

2. Listing the Cron Jobs

This thing is very simple. Just use the option –l with the crontab and it will display all the previously scheduled jobs.

crontab –l
To enlist the cron jobs for a particular user, use the following syntax:

crontab –u [USER] –l 
Example

crontab –u root –l

3. Scheduling a Job

Let’s consider that, we wish to schedule a job which will run only once on July 1 at 19:40 HRS. For that, we need to add following entry to the cron table.

40 19 01 07 * /home/root/backup.sh
If you wish to run the same job two times a day, just separate the values using a comma as follows:

30 08,17 * * * /home/root/backup.sh 
This will run the backup.sh script every day at 8:30 HRS and 17:30 HRS.

Instead, you wish to run the said script every day and on hourly basis,

30 08-17 * * * /home/root/backup.sh
This will execute the script at 8:30 HRS, 9:30 HRS, 10:30 HRS, 11:30 HRS and so on up to 17:30 every day.

If you wish to take the backup on weekends,

00 18 * * 6-7 /home/root/backup.sh 
This script will run at 18:00 HRS on every Saturday and Sunday.

You can change this schedule for the backup to be held during working hours (9 AM to 6 PM) on every weekday.

00 09-18 * * 1-5 /home/root/backup.sh
If you wish the script to be run after a specific interval of time, say 30 minutes in the working time,

*/30 09-18 * * 1-5 /home/root/clean-tmp.sh 
Here, */30 denotes that the script will run after every 30 minutes. Similarly, if you wish to execute the script on every alternate day, you can use 1-5/2 in the DOW field.

This notation holds good for every other field.

4. Making Use of the Keywords

For the script to be executed every year, you can use the keyword @yearly. Note that, the script will run at 00:00 HRS on January 1 of every year.

@yearly /home/root/yearly-backup.sh
Using the keyword @monthly will execute the script at 00:00 HRS on the 1st day of every month.

@monthly /home/root/monthly-backup.sh
If you use the keyword @daily, the script will execute on 00:00 HRS on every day.

@daily /home/root/daily-backup.sh  
The @reboot keyword will execute the mentioned script on every system reboot.

@reboot /home/root/clean-tmp.sh  

5. Notification to a User

Generally, whenever any cron job is accomplished, the output of the said job is sent to the user who had scheduled that particular job. If you wish to redirect the output to any other user, you need to add the MAIL variable in the crontab entry as follows:

MAIL="USERNAME"

@daily /home/root/daily-backup.sh 
If you do not want any notification to be sent to any user, keep the MAIL variable blank as follows:

MAIL="" 

6. Using Crontab Entries from a file

If you prefer not to add the crontab entries using crontab editor, but wish to dump those entries into a file and upload to the cron, you can do it as follows:

mandar@YourOwnLinux:~$ crontab -l 

no crontab for mandar 
mandar@YourOwnLinux:~$ cat my-cron.txt 

@yearly /home/mandar/yearly-backup.sh

* */1 * * * /home/mandar/chkdsk.sh 
mandar@YourOwnLinux:~$ crontab my-cron.txt 
mandar@YourOwnLinux:~$ crontab -l 

@yearly /home/mandar/yearly-backup.sh

* */1 * * * /home/mandar/chkdsk.sh 
One thing to be noticed is that, when the crontab entries are added using a file, like we just did above, it can erase and overwrite to the older entries in the crontab. So, be careful while uploading crontab entries using a file.

2 comments:

  1. i use this script and call it a day "easy cronjob handler"

    http://codecanyon.net/item/the-easycronjobhandler/6296537

    ReplyDelete
  2. I am not very good with linux cron myself so I found this cron job service at www.easycron.com
    I just type in full URL and now its doing the cron job for me.

    ReplyDelete

    Total Pageviews