Senin, 23 November 2009

Belajar Linux - Linux Crond Guide

1. Introduction
Every user as well as administrator of the linux system very often needs to execute some programs on regular basis. For example, an administrator may need to monitor a disk usage of a system. In this case a cron scheduler is very handy tool to achieve this. For example, if root needs to execute /usr/local/sbin/backup.sh script every Sunday at 2:36AM he would edit his crontab file as shown on the figure below:

# crontab -e


The format is simple, 6 fields separated with spaces or tabs. The rest of the line is the command, and it's parameters to be executed. The sixth field - user name (in blue) is used only in the system wide cron scheduler.















2. Cron job examples
Crontab Example 1: This crontab example runs updatedb command 35 minutes past every hour.

35 * * * * updatedb


Crontab Example 2: This crontab example runs /usr/local/bin/diskusage.sh every 5 minutes (e.g. 0, 5, 10, 15, ...).

*/5 * * * * /usr/local/bin/diskusage.sh


Crontab Example 3: This crontab example runs /usr/local/bin/diskusage.sh at 1:25 AM, 1:50 AM every Tuesday and on 15th of every month.

25,50 1 15 * 2 /usr/local/bin/diskusage.sh


Crontab Example 4: This crontab example runs /usr/local/bin/diskusage.sh at 2:00 PM on 10th of March, June, September and December.

00 14 10 3,6,9,12 * /usr/local/bin/diskusage.sh


Crontab Example 5: This crontab example runs '/usr/local/bin/diskusage.sh user@linuxconfig.sh' at 9.00 PM every Monday, Wednesday, Friday. Note: Using names for the week day and months is extension in some versions of crontab.

00 21 * * Mon,Wed,Fri /usr/local/bin/diskusage.sh user@linuxconfig.sh


Crontab Example 6: This crontab example runs /usr/local/bin/diskusage.sh every 5 minutes during the 5 working days (Monday - Friday), every week and month.

*/5 * * * 1-5 /usr/local/bin/diskusage.sh


Crontab Example 7: This crontab example runs /usr/local/bin/diskusage.sh every minute during the 4-th hour only in Sunday, every week and month. This is every minute from 0:00 till 0:59, then from 4:00 till 4:59, etc.

* */4 * * sun /usr/local/bin/diskusage.sh


3. System wide cron scheduler
As a Linux administrator you can also use predefined cron directories:

/etc/cron.d /etc/cron.daily /etc/cron.hourly /etc/cron.monthly /etc/cron.weekly


If root wishes to run backup.sh script once a week he will place backup.sh script into /etc/cron.weekly directory.

4. Cron Scheduler on user level
Every user can edit, view or remove his own crontab file. If the root user needs to change someone else's crontab file he must add '-u' option to specify the user name. To edit crontab file for user foobar we can use command:

# crontab -u foobar -e


Remove foobar's crontab file:

# crontab -u foobar -r


To view foobar's crontab content:

# crontab -u foobar -l

Tidak ada komentar:

Posting Komentar