1. 程式人生 > >crontab定時啟動指令碼,自動化配置

crontab定時啟動指令碼,自動化配置

CRONTAB概念/介紹

      crontab命令用於設定週期性被執行的指令。該命令從標準輸入裝置讀取指令,並將其存放於“crontab”檔案中,以供之後讀取和執行。

cron 系統排程程序。 可以使用它在每天的非高峰負荷時間段執行作業,或在一週或一月中的不同時段執行。cron是系統主要的排程程序,可以在無需人工干預的情況下執行作業。crontab命令允許使用者提交、編輯或刪除相應的作業。每一個使用者都可以有一個crontab檔案來儲存排程資訊。系統管理員可以通過cron.deny 和 cron.allow 這兩個檔案來禁止或允許使用者擁有自己的crontab檔案。

1: crontab檔案一般位於/etc/下,這裡面存放系統執行的的排程程式。

[[email protected] cron]# more /etc/crontab

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

HOME=/

# run-parts

01 * * * * root run-parts /etc/cron.hourly

02 4 * * * root run-parts /etc/cron.daily

22 4 * * 0 root run-parts /etc/cron.weekly

42 4 1 * * root run-parts /etc/cron.monthly

2:每個使用者都會生成一個自動生成一個自己的crontab檔案,一般位於/var/spool/cron目錄下

[[email protected] cron]# cd /var/spool/cron

[[email protected] cron]# ls

oracle root

如果你用命令crontab -r 就會刪除當前使用者的crontab檔案,例如你切換到oracle賬號下,執行了該命令,那麼/var/spool/cron/oracle檔案就會刪除,如果要建立該檔案只需要用crontab -e命令即可。注意,普通使用者一般沒有許可權訪問/var/spool/cron

3:cron.deny 與 cron.allow檔案

/etc/cron.deny 表示不能使用crontab 命令的使用者

/etc/cron.allow 表示能使用crontab的使用者。

預設情況下,cron.allow檔案不存在。如果兩個檔案同時存在,那麼/etc/cron.allow 優先。如果兩個檔案都不存在,那麼只有超級使用者可以安排作業。

但是讓人納悶的是cron.deny檔案是空的,如下所示

[[email protected] etc]$ more cron.deny

4:cron.hourly,cron.daily,cron.monthly,cron.weekly

[[email protected] etc]$ ls -lrt cron*

-rw-r--r-- 1 root root 255 Sep 21 2004 crontab

-rw-r--r-- 1 root root 0 May 16 2008 cron.deny

cron.hourly:

total 0

cron.monthly:

total 8

-rwxr-xr-x 1 root root 278 Sep 29 2004 0anacron

cron.weekly:

total 16

-rwxr-xr-x 1 root root 277 Sep 29 2004 0anacron

-rwxr-xr-x 1 root root 414 Jun 6 2007 00-makewhatis.cron

cron.daily:

total 60

-rwxr-xr-x 1 root root 286 Aug 14 2004 tmpwatch

-rwxr-xr-x 1 root root 276 Sep 29 2004 0anacron

-rwxr-xr-x 1 root root 2133 Dec 1 2004 prelink

-rwxr-xr-x 1 root root 180 May 2 2006 logrotate

-rwxr-xr-x 1 root root 418 Jun 6 2007 00-makewhatis.cron

-rwxr-xr-x 1 root root 104 Aug 2 2007 rpm

-rwxr-xr-x 1 root root 121 Aug 22 2007 slocate.cron

lrwxrwxrwx 1 root root 28 May 16 2008 00-logwatch -> ../log.d/scripts/logwatch.pl

cron.d:

total 12

-rw------- 1 root root 366 Jun 12 2007 sa-update

-rw-r--r-- 1 root root 188 Jul 18 2007 sysstat

CRONTAB線上手冊

注意:不同版本的Linux系統,可能crontab手冊內容有所出入,請以實際版本為準。

[[email protected] ~]$ man crontab | more

複製程式碼

CRONTAB(1)                                                          CRONTAB(1)NAME       crontab - maintain crontab files for individual users (ISC Cron V4.1)SYNOPSIS       crontab [-u user] file       crontab [-u user] [-l | -r | -e] [-i] [-s]DESCRIPTION       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/spool/ , they are not       intended to be edited directly. For SELinux in mls  mode  can  be  even       more crontabs - for each range. For more see selinux(8).       If the cron.allow file exists, then you must be listed therein in order       to be allowed to use this command.  If the  cron.allow  file  does  not       exist but the cron.deny file does exist, then you must not be listed in       the cron.deny file in order to use this command.  If neither  of  these       files  exists, only the super user will be allowed to use this command.OPTIONS       -u     It specifies the name  of  the  user  whose  crontab  is  to  be              tweaked.   If  this option is not given, crontab examines "your"              crontab, i.e., the crontab of the person executing the  command.              Note  that su(8) can confuse crontab and that if you are running              inside of su(8) you should always use the -u option for safety’s              sake.   The  first form of this command is used to install a new              crontab from some named file or standard input  if  the  pseudo-              filename "-" is given.       -l     The current crontab will be displayed on standard output.       -r     The current crontab will be removed.       -e     This option is used to edit the current crontab using the editor              specified by the VISUAL or EDITOR environment variables.   After              you exit from the editor, the modified crontab will be installed              automatically.       -i     This option modifies the -r option to  prompt  the  user  for  a              ’y/Y’ response before actually removing the crontab.       -s     It will append the current SELinux security context string as an              MLS_LEVEL setting to the crontab file before editing /  replace-              ment  occurs - see the documentation of MLS_LEVEL in crontab(5).SEE ALSO       crontab(5), cron(8)FILES       /etc/cron.allow       /etc/cron.denySTANDARDS       The crontab command conforms to IEEE Std1003.2-1992 (‘‘POSIX’’).   This       new  command  syntax  differs  from previous versions of Vixie Cron, as       well as from the classic SVR3 syntax.DIAGNOSTICS       A fairly informative usage message appears if you run  it  with  a  bad       command line.AUTHOR       Paul Vixie <[email protected]>4th Berkeley Distribution       16 Januar 2007                      CRONTAB(1)

複製程式碼

CRONTAB語法及應用

1:檢視當前使用者的定時任務

[[email protected] ~]$ crontab -l

* * * * * /home/oracle/test.sh >/dev/null 2>&1

2:編輯當前使用者的定時任務

可以在編輯狀態修改、刪除、新增一些定時任務。註釋一般用#

[[email protected] ~]$ crontab -e

3:刪除當前使用者的定時任務

[[email protected] ~]# crontab -r

[[email protected] ~]# crontab -l

no crontab for root

4

如下所示,一般crontab檔案裡面的定時任務格式如下所示:

59 23 * * * /home/oracle/scripts/alert_log_archive.sh >/dev/null 2>&1

crontab 檔案中每個條目中各個域的意義和格式:

第一列 分鐘: 1——59

第二列 小時: 1——23(0表示子夜)

第三列 日 : 1——31

第四列 月 : 1——12

第五列 星期: 星期0——6(0表示星期天,1表示星期一、以此類推)

第六列 要執行的命令

我們暫且用C1、C2、C3、C4、C5、C6代表這六列,前面五列通過組合方式來決定執行指令碼的頻率,最小頻率為每分鐘執行一次,其中Cn可以用 * ; */n ; T1-T2; a,b,c; 四種形式來表示:

當 C1 為 * 時表示每分鐘都要執行指令碼,C2 為 * 時表示每小時都要執行程式,依次類推.....

當 C1 為 T1-T2 時表示從第 T1 分鐘到第 T2 分鐘這段時間內要執行,C2 為 T1-T2 時表示從第 T1 到第 T2 小時都要執行,依次類推....

當 C1 為 */n 時表示每 n 分鐘的時間間隔執行一次,C2 為 */n 表示每隔n小時的間隔執行一次,依次類推.....

當 C1 為 a, b, c,... 時表示第 a, b, c,... 分鐘要執行,C2 為 a, b, c,... 時表示第 a, b, c...個小時要執行,依次類推....

下面列舉幾個例子供大家參考

1: 59 23 * * * /home/oracle/scripts/alert_log_archive.sh >/dev/null 2>&1

表示每天23點59分執行指令碼/home/oracle/scripts/alert_log_archive.sh

2: */5 * * * * /home/oracle/scripts/monitoring_alert_log.sh >/dev/null 2>&1

表示每5分鐘執行一次指令碼/home/oracle/scripts/monitoring_alert_log.sh

3: 0 20 * * 1-5 mail -s "**********" [email protected] < /tmp/maildata

週一到週五每天下午 20:00 寄一封信給 [email protected]

..............................................

關於 >/dev/null 2>&1 的解釋:

0表示鍵盤輸入

1表示標準輸出

2表示錯誤輸出.

我們首先建立test.sh指令碼如下:

#! /bin/sh

echo "hello, everybody, now is " `date`

date >> test.txt

然後新增作業

* * * * * /home/oracle/test.sh >/home/oracle/log.txt & 預設值為1,即和下面命令一致

* * * * * /home/oracle/test.sh 1>/home/oracle/log.txt &

* * * * * /home/oracle/test.sh 2>/home/oracle/log.txt &

* * * * * /home/oracle/test.sh 2>/home/oracle/log.txt 2>&1 &

1,2將tesh.sh 命令輸出重定向到log.txt, 即輸出內容不列印到螢幕上,而是輸出到log.txt檔案中。如果你需要追加而不是覆蓋,可以用 >>代替>

2>&1 是將錯誤輸出重定向到標準輸出。 然後將標準輸入重定向到檔案log.txt。

&1 表示的是檔案描述1,表示標準輸出,如果這裡少了&就成了數字1,就表示重定向到檔案1。

注意事項:

配置定時任務時,需要注意兩個問題:

1: 在SHELL中設定了必要的環境變數;例如一個shell指令碼手工執行OK,但是配置成後臺作業執行時,獲取不到ORACLE的環境變數,這是因為crontab環境變數問題,Crontab的環境預設情況下並不包含系統中當前使用者的環境。所以,你需要在shell指令碼中新增必要的環境變數的設定

2: 儘量所有的檔案都採用完全路徑方式,避免使用相對路徑。

參考資料:

http://blog.csdn.net/tianlesoftware/article/details/5315039

http://yke2000.blog.163.com/blog/static/319238142010147308843/

作者:瀟湘隱者