linux cron三言兩語

shiri512003發表於2010-04-13

作者: 絃樂之花 | 可以轉載, 但必須以超連結形式標明文章原始出處和作者資訊及版權宣告

http://shiri512003.itpub.net/post/37713/499162

[@more@]

crontab對於dba而言應該是很熟悉的了,然而crontab到底做什麼的呢?

crontab的man:

Crontab is the program used to install, deinstall or list the tables used to drive the cron(8) daemon in ISC Cron.Each user can have their own crontab, and though these are files in /var, they are not intended to be edited directly.

事實上crontab只是個傳話的工具而已,或者只是個秘書,仵作,負責將使用者想要定時執行的認為資訊記錄到相應的檔案中。

真正負責執行認為的是cron - daemon to execute scheduled commands (ISC Cron V4.1)。

有時候為了測試指令碼是否可以正常被cron排程執行,通常會手工在crontab裡面編輯錄入需要測試的指令碼,而為了儘快得到結果,會把排程時間寫成下一分鐘,這時候也許會發生些意外,明明手工可以執行的腳步,明明可以確定和環境變數無關的腳步,卻偏偏沒有被排程,難到指令碼有問題??

事實上看下cron的man

Cron searches /var/spool/cron for crontab files which are named after accounts in /etc/passwd; crontabs found are loaded into memory. Cron also searches for /etc/crontab and the files in the /etc/cron.d directory, which are in a different format (see crontab(5)). Cron then wakes up every minute, examining all stored crontabs, checking each command to see if it should be run in the current minute. When execut-ing commands, any output is mailed to the owner of the crontab (or to the user named in the MAILTO environment variable in the crontab,if such exists).Additionally, cron checks each minute to see if its spool directory's modtime (or the modtime on /etc/crontab) has changed, and if it has,cron will then examine the modtime on all crontabs and reload those which have changed. Thus cron need not be restarted whenever a crontab file is modified. Note that the Crontab(1) command updates the modtime of the spool directory whenever it changes a crontab.

1分鐘一次的排程週期,如果剛好不幸錯過了排程週期,那就會使指令碼排程失敗了。所以除錯的時候至少要設定排程時間為sysdate+(1~2)min。

補充下,AIX下面cron的行為和linux有所不同,When you make changes to the crontab files using the crontab command, a message indicating the change is sent to the cron daemon. This eliminates the overhead of checking for new or changed files at regularly scheduled intervals.

再補充點crontab裡設定環境變數

[oracle@db-test4 ~]$ crontab -l|grep my.log
24 * * * * w=hi;echo $w >>/home/oracle/my.log

[oracle@db-test4~]$ more my.log
hi

cron預設env

HOME=user’s-home-directory
LOGNAME=user’s-login-id
PATH=/usr/bin:/usr/sbin:.
SHELL=/usr/bin/sh

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7591490/viewspace-1032826/,如需轉載,請註明出處,否則將追究法律責任。

相關文章