Crontab定時任務配置


CRONTAB概念/介紹

      crontab命令用於設置周期性被執行的指令。該命令從標准輸入設備讀取指令,並將其存放於“crontab”文件中,以供之后讀取和執行。

cron 系統調度進程。 可以使用它在每天的非高峰負荷時間段運行作業,或在一周或一月中的不同時段運行。cron是系統主要的調度進程,可以在無需人工干預的情況下運行作業。crontab命令允許用戶提交、編輯或刪除相應的作業。每一個用戶都可以有一個crontab文件來保存調度信息。系統管理員可以通過cron.deny 和 cron.allow 這兩個文件來禁止或允許用戶擁有自己的crontab文件。

1: crontab文件一般位於/etc/下,這里面存放系統運行的的調度程序。

[root@localhost 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目錄下

[root@localhost cron]# cd /var/spool/cron

[root@localhost 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文件是空的,如下所示

[oracle@localhost etc]$ more cron.deny

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

[oracle@localhost 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手冊內容有所出入,請以實際版本為准。

[oracle@localhost ~]$ 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.deny

STANDARDS
       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 <vixie@isc.org>



4th Berkeley Distribution       16 Januar 2007                      CRONTAB(1)
View Code

 

CRONTAB語法及應用

 

1:查看當前用戶的定時任務

[oracle@localhost ~]$ crontab -l

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

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

 

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

[oracle@localhost ~]$ crontab -e

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

[root@localhost ~]# crontab -r

[root@localhost ~]# 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 "**********" kerry@domain.name < /tmp/maildata

周一到周五每天下午 20:00 寄一封信給 kerry@domain.name

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

關於 >/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/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM