欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2016年11月30日 10:32:39   投稿:jingxian  
下面小編就為大家?guī)硪黄狶inux crontab定時任務(wù)配置方法(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

CRONTAB概念/介紹

crontab命令用于設(shè)置周期性被執(zhí)行的指令。該命令從標(biāo)準輸入設(shè)備讀取指令,并將其存放于“crontab”文件中,以供之后讀取和執(zhí)行。

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

1: crontab文件一般位于/etc/下,這里面存放系統(tǒng)運行的的調(diào)度程序。

[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 就會刪除當(dāng)前用戶的crontab文件,例如你切換到oracle賬號下,執(zhí)行了該命令,那么/var/spool/cron/oracle文件就會刪除,如果要創(chuàng)建該文件只需要用crontab -e命令即可。注意,普通用戶一般沒有權(quán)限訪問/var/spool/cron

3:cron.deny 與 cron.allow文件

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

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

默認情況下,cron.allow文件不存在。如果兩個文件同時存在,那么/etc/cron.allow 優(yōu)先。如果兩個文件都不存在,那么只有超級用戶可以安排作業(yè)。

但是讓人納悶的是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系統(tǒng),可能crontab手冊內(nèi)容有所出入,請以實際版本為準。

[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)

CRONTAB語法及應(yīng)用

1:查看當(dāng)前用戶的定時任務(wù)

[oracle@localhost ~]$ crontab -l

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

2:編輯當(dāng)前用戶的定時任務(wù)

可以在編輯狀態(tài)修改、刪除、新增一些定時任務(wù)。注釋一般用#

[oracle@localhost ~]$ crontab -e

3:刪除當(dāng)前用戶的定時任務(wù)

[root@localhost ~]# crontab -r

[root@localhost ~]# crontab -l

no crontab for root

4:

如下所示,一般crontab文件里面的定時任務(wù)格式如下所示:

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代表這六列,前面五列通過組合方式來決定執(zhí)行腳本的頻率,最小頻率為每分鐘執(zhí)行一次,其中Cn可以用 * ; */n ; T1-T2; a,b,c; 四種形式來表示:

當(dāng) C1 為 * 時表示每分鐘都要執(zhí)行腳本,C2 為 * 時表示每小時都要執(zhí)行程式,依次類推.....

當(dāng) C1 為 T1-T2 時表示從第 T1 分鐘到第 T2 分鐘這段時間內(nèi)要執(zhí)行,C2 為 T1-T2 時表示從第 T1 到第 T2 小時都要執(zhí)行,依次類推....

當(dāng) C1 為 */n 時表示每 n 分鐘的時間間隔執(zhí)行一次,C2 為 */n 表示每隔n小時的間隔執(zhí)行一次,依次類推.....

當(dāng) C1 為 a, b, c,... 時表示第 a, b, c,... 分鐘要執(zhí)行,C2 為 a, b, c,... 時表示第 a, b, c...個小時要執(zhí)行,依次類推....

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

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

表示每天23點59分執(zhí)行腳本/home/oracle/scripts/alert_log_archive.sh

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

表示每5分鐘執(zhí)行一次腳本/home/oracle/scripts/monitoring_alert_log.sh

3: 0 20 * * 1-5 mail -s "**********" kerry@domain.name < /tmp/maildata

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

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

關(guān)于 >/dev/null 2>&1 的解釋:

0表示鍵盤輸入

1表示標(biāo)準輸出

2表示錯誤輸出.

我們首先創(chuàng)建test.sh腳本如下:

#! /bin/sh

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

date >> test.txt

然后添加作業(yè)

* * * * * /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, 即輸出內(nèi)容不打印到屏幕上,而是輸出到log.txt文件中。如果你需要追加而不是覆蓋,可以用 >>代替>

2>&1 是將錯誤輸出重定向到標(biāo)準輸出。 然后將標(biāo)準輸入重定向到文件log.txt。

&1 表示的是文件描述1,表示標(biāo)準輸出,如果這里少了&就成了數(shù)字1,就表示重定向到文件1。

注意事項:

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

1: 在SHELL中設(shè)置了必要的環(huán)境變量;例如一個shell腳本手工執(zhí)行OK,但是配置成后臺作業(yè)執(zhí)行時,獲取不到ORACLE的環(huán)境變量,這是因為crontab環(huán)境變量問題,Crontab的環(huán)境默認情況下并不包含系統(tǒng)中當(dāng)前用戶的環(huán)境。所以,你需要在shell腳本中添加必要的環(huán)境變量的設(shè)置

2: 盡量所有的文件都采用完全路徑方式,避免使用相對路徑。

以上這篇Linux crontab定時任務(wù)配置方法(詳解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Linux系統(tǒng)下部署項目的設(shè)置方法

    Linux系統(tǒng)下部署項目的設(shè)置方法

    這篇文章主要介紹了Linux系統(tǒng)下部署項目的教程詳解,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-10-10
  • 使用命令行檢測Ubuntu版本方法

    使用命令行檢測Ubuntu版本方法

    在本篇文章中小編給大家整理了關(guān)于使用命令行檢測Ubuntu版本方法和相關(guān)代碼,有興趣的朋友們學(xué)習(xí)下。
    2019-03-03
  • ubuntu下apache服務(wù)器操作方法小結(jié)

    ubuntu下apache服務(wù)器操作方法小結(jié)

    這篇文章主要介紹了ubuntu下apache服務(wù)器操作方法小結(jié),非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-12-12
  • Linux如何實現(xiàn)斷點續(xù)傳文件功能

    Linux如何實現(xiàn)斷點續(xù)傳文件功能

    最近在工作中遇到一個需求,要實現(xiàn)Linux下的文件傳輸,支持斷點續(xù)傳,所以這篇文章主要給大家介紹了關(guān)于Linux如何實現(xiàn)斷點續(xù)傳文件功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-12-12
  • Linux VPS配置Web網(wǎng)站環(huán)境一鍵包(LNMP/LAMP/LNMPA)

    Linux VPS配置Web網(wǎng)站環(huán)境一鍵包(LNMP/LAMP/LNMPA)

    如果我們是資深Linux用戶,可能不屑于網(wǎng)上免費Linux Web一鍵包、管理面板的安裝,然后自己編譯或者自由的一套環(huán)境安裝配置環(huán)境。但是,對于大部分用戶而言,麥子個人建議還是選擇較為成熟的WEB一鍵包或者面板安裝環(huán)境
    2017-02-02
  • Linux IO多路復(fù)用之epoll網(wǎng)絡(luò)編程

    Linux IO多路復(fù)用之epoll網(wǎng)絡(luò)編程

    今天小編就為大家分享一篇關(guān)于Linux IO多路復(fù)用之epoll網(wǎng)絡(luò)編程,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • 詳解linux下避免僵尸進程的幾種方法

    詳解linux下避免僵尸進程的幾種方法

    本篇文章主要介紹了詳解linux下避免僵尸進程的幾種方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • Linux硬盤合并的實現(xiàn)代碼

    Linux硬盤合并的實現(xiàn)代碼

    這篇文章主要介紹了Linux硬盤合并的實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • 詳細CentOS-6.3安裝配置SVN的方法

    詳細CentOS-6.3安裝配置SVN的方法

    本文為大家介紹在CentOS-6.3中安裝與配置SVN的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考
    2016-12-12
  • 使用Hyper-v虛擬機安裝Centos7

    使用Hyper-v虛擬機安裝Centos7

    本文詳細講解了使用Hyper-v虛擬機安裝Centos7的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-12-12

最新評論