Linux crontab定時任務配置方法(詳解)_Linux

來源:互聯網
上載者:User

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.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 <vixie@isc.org>4th Berkeley Distribution    16 Januar 2007           CRONTAB(1)

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: 盡量所有的檔案都採用完全路徑方式,避免使用相對路徑。

以上這篇Linux crontab定時任務配置方法(詳解)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.