在Unix和Solaris環(huán)境下建立Cron Jobs
Cron是Unix, solaris工具, 允許cron精靈進(jìn)程在后臺(tái)每隔一段時(shí)間自動(dòng)調(diào)度運(yùn)行任務(wù)。這些任務(wù)在Unix solaris環(huán)境中稱為cron jobs, Crontab(Cron表)是一個(gè)文件,里面包含了要在特定時(shí)間運(yùn)行的Cron的條目。
這個(gè)文檔中覆蓋了Unix cron jobs以下幾個(gè)方面
1. Crontab限制
2. Crontab 命令
3. Crontab 文件語法
4. Crontab 實(shí)例
5. Crontab 環(huán)境
6. 禁止發(fā)送郵件
7. 產(chǎn)生crontab 行為的Log 文件
1.Crontab限制
如果你的名字在文件/usr/lib/cron/cron.allow.中,那么你可以執(zhí)行crontab,如果該文件不存在,但是你的名字不在文件/usr/lib/cron/cron.deny,你還是可以使用crontab。 如果cron.deny存在并且為空,那么所有人都可以使用crontab,如果文件都不存在,那么只有root用戶可以使用crontab,allow/deny文件中每行包含一個(gè)用戶名稱。
2.Crontab命令
export EDITOR=vi 用于指定一個(gè)編輯器來打開crontab文件
crontab -e 編輯crontab文件,如果不存在就創(chuàng)建一個(gè)
crontab –l 顯示crontab文件內(nèi)容
crontab –r 移除crontab文件
crontab –v 顯示你最后一個(gè)編輯crontab的時(shí)間(該選項(xiàng)只存在于少量系統(tǒng)中)
3.Crontab 文件
Crontab 語法:
Crontab文件有5個(gè)字段用于指定天,日期和時(shí)間,緊跟著是要運(yùn)行的命令
上面的值域中的*是指相應(yīng)的字段所有的合法值。值字段中可以有一個(gè)*或者一個(gè)逗號(hào)分割的元素值的列表。每一個(gè)元素或者是上面顯示的范圍中的一個(gè)數(shù)字,或者是范圍中用-分割的兩個(gè)數(shù)字。
* * * * * 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)
|
要點(diǎn)
A)每?jī)煞昼?/span> /2, 10分鐘 /10這樣的重復(fù)匹配模式在很多的操作系統(tǒng)不被支持。 如果你試圖使用,而crontab不接受,那么它可能不支持。
B)標(biāo)準(zhǔn)中描述的天可以是以下的兩個(gè)域,月中的天和周中的天。如果條目中指定了這兩個(gè),就顯得累積,意味著兩個(gè)條目都會(huì)得到執(zhí)行。
4.Crontab實(shí)例
像下面這樣Crontab中的文件行意思是在每天下午的6:30移除/home/someuser/tmp中的文件
30 18 * * * rm /home/someuser/tmp/*
如下述列表中一樣改變參數(shù)的值可以使命令在不同的時(shí)間被調(diào)度
min |
hour |
day/month |
month |
day/week |
Execution time |
30 |
0 |
1 |
1,6,12 |
* |
– 00:30 Hrs on 1st of Jan, June & Dec. |
|
0 |
20 |
* |
10 |
1-5 |
–8.00 PM every weekday (Mon-Fri) only in Oct. |
|
0 |
0 |
1,10,15 |
* |
* |
– midnight on 1st ,10th & 15th of month |
|
5,10 |
0 |
10 |
* |
1 |
– At 12.05,12.10 every Monday & on 10th of every month |
: |
注意:如果你不經(jīng)意間輸入了沒有參數(shù)的crontab命令,不要試圖按Control-d退出,這樣會(huì)刪除文件中的所有條目,應(yīng)該使用Control-c退出
5. Crontab環(huán)境
Cron使用shell(/usr/bin/sh)從用戶的home目錄中調(diào)用命令。
Cron為每一個(gè)shell提供了一個(gè)缺省的環(huán)境,定義如下
HOME=user’s-home-directory
LOGNAME=user’s-login-id
PATH=/usr/bin:/usr/sbin:.
SHELL=/usr/bin/sh
用戶想要執(zhí)行他們自己的.profile必須顯式的在crontab 條目中,或者條目調(diào)用的相關(guān)腳本中做上面的設(shè)置
6.禁止發(fā)送郵件
默認(rèn)情況下,cron jobs發(fā)送一封郵件給執(zhí)行cronjob的用戶帳戶,如果不需要,在cronjob末尾的文件行鍵入以下命令
>/dev/null 2>&1
7. 產(chǎn)生相關(guān)的log文件
收集cron執(zhí)行的相關(guān)log并輸出到文件中
30 18 * * * rm /home/someuser/tmp/* > /home/someuser/cronlogs/clean_tmp_dir.log
Setting up cron jobs in Unix and Solaris
cron is a unix, solaris utility that allows tasks to be automatically run in the background at regular intervals by the cron daemon. These tasks are often termed as cron jobs in unix , solaris. Crontab (CRON TABle) is a file which contains the schedule of cron entries to be run and at specified times.
This document covers following aspects of Unix cron jobs
1. Crontab Restrictions
2. Crontab Commands
3. Crontab file – syntax
4. Crontab Example
5. Crontab Environment
6. Disable Email
7. Generate log file for crontab activity
1. Crontab Restrictions
You can execute crontab if your name appears in the file /usr/lib/cron/cron.allow. If that file does not exist, you can use
crontab if your name does not appear in the file /usr/lib/cron/cron.deny.
If only cron.deny exists and is empty, all users can use crontab. If neither file exists, only the root user can use crontab. The allow/deny files consist of one user name per line.
2. Crontab Commands
export EDITOR=vi ;to specify a editor to open crontab file.
crontab -e Edit your crontab file, or create one if it doesn’t already exist.
crontab -l Display your crontab file.
crontab -r Remove your crontab file.
crontab -v Display the last time you edited your crontab file. (This option is only available on a few systems.)
3. Crontab file
Crontab syntax :
A crontab file has five fields for specifying day , date and time followed by the command to be run at that interval.
* * * * * 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)
|
* in the value field above means all legal values as in braces for that column.
The value column can have a * or a list of elements separated by commas. An element is either a number in the ranges shown above or two numbers in the range separated by a hyphen (meaning an inclusive range).
Notes
A. ) Repeat pattern like /2 for every 2 minutes or /10 for every 10 minutes is not supported by all operating systems. If you try to use it and crontab complains it is probably not supported.
B.) The specification of days can be made in two fields: month day and weekday. If both are specified in an entry, they are cumulative meaning both of the entries will get executed .
4. Crontab Example
A line in crontab file like below removes the tmp files from /home/someuser/tmp each day at 6:30 PM.
30 18 * * * rm /home/someuser/tmp/*
Changing the parameter values as below will cause this command to run at different time schedule below :
min |
hour |
day/month |
month |
day/week |
Execution time |
30 |
0 |
1 |
1,6,12 |
* |
– 00:30 Hrs on 1st of Jan, June & Dec. |
|
0 |
20 |
* |
10 |
1-5 |
–8.00 PM every weekday (Mon-Fri) only in Oct. |
|
0 |
0 |
1,10,15 |
* |
* |
– midnight on 1st ,10th & 15th of month |
|
5,10 |
0 |
10 |
* |
1 |
– At 12.05,12.10 every Monday & on 10th of every month |
: |
Note : If you inadvertently enter the crontab command with no argument(s), do not attempt to get out with Control-d. This removes all entries in your crontab file. Instead, exit with Control-c.
5. Crontab Environment
cron invokes the command from the user’s HOME directory with the shell, (/usr/bin/sh).
cron supplies a default environment for every shell, defining:
HOME=user’s-home-directory
LOGNAME=user’s-login-id
PATH=/usr/bin:/usr/sbin:.
SHELL=/usr/bin/sh
Users who desire to have their .profile executed must explicitly do so in the crontab entry or in a script called by the entry.
6. Disable Email
By default cron jobs sends a email to the user account executing the cronjob. If this is not needed put the following command At the end of the cron job line .
>/dev/null 2>&1
7. Generate log file
To collect the cron execution execution log in a file :
30 18 * * * rm /home/someuser/tmp/* > /home/someuser/cronlogs/clean_tmp_dir.log