Sites

Wikipedia
SlashDot
Gentoo Linux
Loop-AES Encryption
Geoiptool

Thursday, July 2, 2009

Understanding Cron Jobs.

Crontab
The command to create/edit, list, and remove cron jobs is crontab. If you call it with the -u option, it specifies the name of the user whose crontab is to be edited. If this option is not given, crontab examines "your" crontab, i.e., the crontab of the user executing the command. If you are looged in as root and run crontab without -u, then root's crontab is listed/modified/removed. If you are logged in as test and run crontab without -u, then test's crontab is listed/modified/removed.

Some Examples:

crontab -l

lists the cron jobs of the user as that you are currently logged in:


test:~# crontab -l
* * * * * /usr/local/examplescript.sh > /dev/null 2>> /var/log/cron.log
30 00 * * * /usr/local/examplescript2.sh > /dev/null 2>> /var/log/cron.log
test:~#

crontab -u exampleuser -l (lists all cron jobs of exampleuser.)

crontab -e (let's you create/modify the cron jobs of the user as that you are currently logged in.)

crontab -u exampleuser -e (let's you create/modify the cron jobs of exampleuser.)

crontab -r (deletes all cron jobs of the user as that you're currently logged in.)

crontab -u exampleuser -r (deletes all cron jobs of exampleuser.)

Cronjob Syntax



A cron job consists out of six fields:

minute, hour ,day of month, month day of week, YourCommand

field allowed values
----- --------------
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names, see below)
day of week 0-7 (0 or 7 is Sun, or use names)


When specifying day of week, both day 0 and day 7 will be considered Sunday.

A field may be an asterisk (*), which always stands for first-last.

Names can also be used for the "month" and "day of week" fields. Use the first three letters of the particular day or month (case doesn't matter), e.g. mon or MON for Monday or aug / AUG for August.

Lets look at these examples:


* * * * * /usr/local/example.sh > /dev/null 2>> /var/log/cron.log

This means: execute /usr/local/example.sh > /dev/null 2>> /var/log/cron.log once per minute.

30 00 * * * /usr/local/cron_daily.sh > /dev/null 2>> /var/log/cron.log

This means: execute /usr/local/cron_daily.sh > /dev/null 2>> /var/log/cron.log once per day at 00:30h.

You can also use ranges to define cron jobs:

Examples:

1,2,5,9 - means every first, second, fifth, and ninth (minute, hour, month, ...).

0-4,8-12 - means all (minutes, hours, months,...) from 0 to 4 and from 8 to 12.

*/5 - means every fifth (minute, hour, month, ...).

1-9/2 is the same as 1,3,5,7,9.

Ranges or lists of names are not allowed (if you are using names instead of numbers for months and days - e.g., Mon-Wed is not valid).

1,7,25,47 */2 * * * command

means: run command every second hour in the first, seventh, 25th, and 47th minute.

Instead of the first five fields, one of eight special strings may appear:


string meaning

@reboot Run once, at startup.
@yearly Run once a year, "0 0 1 1 *".
@annually (same as @yearly)
@monthly Run once a month, "0 0 1 * *".
@weekly Run once a week, "0 0 * * 0".
@daily Run once a day, "0 0 * * *".
@midnight (same as @daily)
@hourly Run once an hour, "0 * * * *".



Bookmark and Share

No comments:

Post a Comment