Overview:For commands that need to be executed repeatedly (e.g., hourly, daily, or weekly), you can use the crontab command. The crontab command creates a crontab file containing commands and instructions for the cron daemon to execute. You can use the crontab command with the following options:
Each entry in a crontab file consists of six fields, specifying in the following order: minute(s) hour(s) day(s) month(s) weekday(s) command(s) The fields are separated by spaces or tabs. The first five are integer patterns and the sixth is the command to execute. The following table briefly describes each of the fields:
Each of the patterns from the first five fields may be either * (an asterisk), meaning all legal values, or a list of elements separated by commas. An element is either a number or an inclusive range, indicated by two numbers separated by a minus sign (e.g., 10-12). You can specify days with two fields: day of the month and day of the week. If you specify both of them as a list of elements, cron will observe both of them, for example: Easy format: * * * * * command to be executed - - - - - | | | | | | | | | ----- Day of week (0 - 7) (Sunday=0 or 7) | | | ------- Month (1 - 12) | | --------- Day of month (1 - 31) | ----------- Hour (0 - 23) ------------- Minute (0 - 59) To list all your cron jobs, type the following command: # crontab -l # crontab -u username -l Instead of the first five fields, you can use any one of eight special strings. It will not just save your time but it will improve readability.
Examples:Edit your own crontab file via the command $ crontab -e The cron daemon would run the program myprogram in the mydir directory on the first and fifteenth of each month, as well as on every Monday. To specify days by only one field, the other field should be set to *, for example: 0 0 1,15 * 1 /mydir/myprogram The program would run only on Mondays. 0 0 * * 1 /mydir/myprogram */5 * * * * /path/to/job To run /path/to/command five minutes after midnight, every day, enter: 5 0 * * * /path/to/command Run /path/to/script.sh at 2:15pm on the first of every month, enter: 15 14 1 * * /path/to/script.sh Run /scripts/phpscript.php at 10 pm on weekdays, enter: 0 22 * * 1-5 /scripts/phpscript.php Run /root/scripts/perl/perlscript.pl at 23 minutes after midnight, 2am, 4am …, everyday, enter: 23 0-23/2 * * * /root/scripts/perl/perlscript.pl Run /path/to/unixcommand at 5 after 4 every Sunday, enter: 5 4 * * sun /path/to/unixcommand References:
|
Home > unix/linux >