Wednesday, April 9, 2014

Linux Crontab

How to set-up a crontab file ?

In Linux, Cron is a daemon/service that executes shell commands periodically on a given schedule. Cron is driven by a crontab, a configuration file that holds details of what commands are to be run along with a timetable of when to run them.

Creating a crontab file

You can create a crontab file by entering the following terminal command:
crontab -e

Entering the above command will open a terminal editor with a new blank crontab file, or it will open an existing crontab if you already have one. You can now enter the commands to be executed, see syntax below, before saving the file and exiting the editor. As long as your entries were entered correctly your commands should now be executed at the times/dates you specified. You can see a list of active crontab entries by entering the following terminal command:

crontab -l


Crontab Syntax

A crontab file has six fields for specifying minute, hour, day of month, month, day of week and the command to be run at that interval. See below:

*     *     *     *     *  command to be executed
-     -     -     -     -
|     |     |     |     |
|     |     |     |     +----- day of week (0 - 6) (Sunday=0)
|     |     |     +------- month (1 - 12)
|     |     +--------- day of month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)

My requirement is to generate a report in excel file every 15 minutes. So, I have a jar say Reports.jar file to be executed. 

Schedule a Background Cron Job For Every 15 Minutes.

*/15 * * * * /usr/java/jdk1.6.0_34/bin/java -jar ./Reports.jar >> ./cronlog.txt

*/15 - Minute to run on, a value of 0-59: /15 means every 15 minutes
*       - The Hour, a value of 0-23: * means every hour
*       - Day of the Month, 1-31: * means every day of the month
*       - The Month, 1-12: * means every month
*       - Day of the Week, 0-7 (0 and 7 are both Sunday): * means every day
The Command: /usr/java/jdk1.6.0_34/bin/java -jar ./Reports.jar >> ./cronlog.txt execute jar and write result to cronlog.txt file

No comments:

Post a Comment