crond定時任務詳細分析

一、定時任務crond的介紹  

crond是linux系統中用來定期執行命令或指定程序任務的一種服務或軟件。一般情況下,我們安裝文成系統之後,默認變回啓動crond任務調度服務,crond服務會定期(默認每分鐘檢查一次)檢查系統中是否有要執行的任務工作。如果有,變會根據預先設定的定時任務自動執行該定時任務,就如同生活中的鬧鐘一樣。

[root@iZ94lm56da9Z ~]# chkconfig --list|grep crond    <--查詢是否開啓
crond              0:off    1:off    2:on    3:on    4:on    5:on    6:off
[root@iZ94lm56da9Z ~]# crontab -l    <--查詢crond的配置
no crontab for root

因爲crond是每分鐘級別,如果crond服務搞不定了,一般工作中寫腳本守護程序進程:

[root@iZ94lm56da9Z ~]# cat corn 
while true
do
    echo "wangde .....Rui"
    sleep 1
done
#內容意思爲每秒echo出"wangde .....Rui"。

補充兩個概念:

    程序文件:程序代碼組成,但是沒有在計算機內執行。當前沒有執行。

    守護程序或者守護進程:進程就是在計算機中正在執行的程序;守護進程就是一直運行的程序。

可以看出定時任務是一個守護進程。

[root@iZ94lm56da9Z ~]# ps -ef |grep crond
root       923     1  0 Aug25 ?        00:00:00 crond
root     27961 27915  0 15:29 pts/0    00:00:00 grep crond


crond和ssh一樣是一直在後臺運行的守護進程:

[root@iZ94lm56da9Z ~]# /etc/init.d/sshd restart
Stopping sshd:                                             [  OK  ]
Starting sshd:                                             [  OK  ]
[root@iZ94lm56da9Z ~]# /etc/init.d/crond restart
Stopping crond:                                            [  OK  ]
Starting crond:                                            [  OK  ]

二、不同系統的定時任務

windows的定時任務存放位置:所有程序->附件->系統工具->任務計劃程序

wKioL1fBQ4vjyAYrAAFn6ep4z1E488.jpg-wh_50

Linux系統的定時任務調度的工作可以分爲以下兩種情況:

    1.linux系統自身定期執行的任務工作:系統週期性的自行執行的任務工作,例如輪循備份系統日誌、備份系統數據、清理系統緩存等,這些任務無需我們人爲干預。如下:

[root@alone ~]# ll /var/log/messages*
-rw-------  1 root root 113765 8月  23 23:49 /var/log/messages
-rw-------. 1 root root 226986 8月   7 02:58 /var/log/messages-20160807
-rw-------  1 root root  25713 8月  14 02:58 /var/log/messages-20160814
-rw-------  1 root root  25649 8月  21 02:58 /var/log/messages-20160821
[root@alone ~]# ll /var/log/secure*
-rw-------  1 root root  43166 8月  23 23:44 /var/log/secure
-rw-------. 1 root root  91198 8月   7 03:16 /var/log/secure-20160807
-rw-------  1 root root 133631 8月  14 03:46 /var/log/secure-20160814
-rw-------  1 root root 134102 8月  21 03:30 /var/log/secure-20160821
#每週一份日誌,系統自身執行的定時任務

系統默認執行的日誌腳本存放位置:

[root@alone ~]# cd /etc/logrotate.d/
[root@alone logrotate.d]# pwd
/etc/logrotate.d
[root@alone logrotate.d]# ls -lrt
總用量 32
-rw-r--r--. 1 root root 136 8月  23 2010 ppp
-rw-r--r--. 1 root root 329 7月  17 2012 psacct
-rw-r--r--. 1 root root 100 2月   4 2013 wpa_supplicant
-rw-r--r--. 1 root root 100 2月  22 2013 yum
-rw-r--r--. 1 root root 210 8月  15 2013 syslog
-rw-r--r--. 1 root root  71 8月  17 2013 cups
-rw-r--r--. 1 root root 219 11月 23 2013 sssd
-rw-r--r--. 1 root root 103 11月 26 2013 dracut
[root@alone logrotate.d]# 
[root@alone logrotate.d]# less syslog 
[root@alone logrotate.d]# cat syslog |grep -v '^$'
/var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
{
    sharedscripts
    postrotate
    /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
    endscript
}

系統自動輪循任務的設置配置路徑:

[root@alone logrotate.d]# ll /etc/ |grep cron
-rw-------.  1 root root    541 11月 23 2013 anacrontab
drwxr-xr-x.  2 root root   4096 7月  27 13:14 cron.d
drwxr-xr-x.  2 root root   4096 7月  27 13:17 cron.daily
-rw-------.  1 root root      0 11月 23 2013 cron.deny
drwxr-xr-x.  2 root root   4096 7月  27 13:13 cron.hourly
drwxr-xr-x.  2 root root   4096 7月  27 13:15 cron.monthly
-rw-r--r--.  1 root root    457 9月  27 2011 crontab
drwxr-xr-x.  2 root root   4096 9月  27 2011 cron.weekly
[root@alone logrotate.d]# pwd
/etc/logrotate.d

以下是系統及的定時任務,一般用戶自定義的定時任務不會放在這裏:

[root@alone logrotate.d]# cat /etc/crontab 
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed

    2.用戶執行的定時任務:某個用戶或者系統管理員定期要做的任務工作,例如每個5分鐘和互聯網時間服務器同步(當然這個是最基礎的系統優化),每天晚上0點備份站點數據及數據庫數據,這些工作一般都是用戶自己設定定時任務才行,

例如:服務器時間同步:

[root@iZ94lm56da9Z ~]# crontab -l
*/5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1

三、linux系統下定時任務軟件的種類

linux系統下基本有以下幾種:at(適合只執行一次)、crontab(最常用)、anacron。

    at:適合執行一次就借宿的調度任務命令。例如:在今晚需要處理一個任務,僅僅是今天晚上,屬於突發性的工作任務。要執行at命令,還需要啓動一個名爲atd的服務才行。(在工作中不常用,所以默認爲關閉狀態)

[root@iZ94lm56da9Z ~]# chkconfig --list |grep atd
atd                0:off    1:off    2:off    3:off    4:off    5:off    6:off

    anacron:這個命令主要用於非7*24小時開機的服務器準備的,anacron並不能指定具體時間執行任務,而是以天爲週期或者在系統每次開機後執行的任務工作。他會檢測服務器停機期間應該執行但沒有進行的任務工作,並將該任務執行一遍。(工作中一般服務器是持續開機,所以一般不怎麼用)

    備註:

1.這裏的crond服務是運行的程序,而crontab命令用戶用來設置定時規則的命令

2.crond服務是工作衝常用的重要服務,at和anacron很少使用,可以忽略,

3.工作中幾乎每個服務器都會用到crond服務。



四、crontab命令的格式 (下面進入主題)

crond    守護進程,一直運行着的

crontab    設置命令,-e 編輯;-l 列表。

[root@alone logrotate.d]# crontab -h
crontab:無效選項 -- h
crontab: usage error: unrecognized option
usage:    crontab [-u user] file
    crontab [-u user] [ -e | -l | -r ]
        (default operation is replace, per 1003.2)
    -e    (edit user's crontab)
    -l    (list user's crontab)
    -r    (delete user's crontab)
    -i    (prompt before deleting user's crontab)
    -s    (selinux context)

crontab -e  相當於 vi /var/spool/cron/root

crontab -l   相當於 cat /var/spool/cron/root

[root@iZ94lm56da9Z ~]# crontab -l
*/5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1
[root@iZ94lm56da9Z ~]# cat /var/spool/cron/root 
*/5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1

讓普通用戶執行crontab -u richy -e   或者 切換到richy用戶下 再使用crontab


crontab 定時任務命令字段解釋

*           *         *         *           *

0-59   0-23   1-31   1-12     0-6

*  每   * * * * * /bin/sh/scripts/richy.sh  每分鐘執行richy.sh

-  範圍    00 17-19 * * * /bin/sh/scripts/richy.sh  每天17點,18點,19點整執行richy.sh

, 分隔   30 17,18,19 * * * /bin/sh/scripts/richy.sh  每天17:30,18:30,19:30執行richy.sh

/n  每單位時間   */10 * * * * /bin/sh/scripts/richy.sh  每10分鐘執行richy.sh


30 3-5 17-19 * * /bin/sh/scripts/richy.sh  每月的17,18,19號的3:30,4:30,5:30執行命令richy.sh

五、crontab編輯定時任務依賴的服務

[root@iZ94lm56da9Z ~]# chkconfig --list |grep crond
crond              0:off    1:off    2:on    3:on    4:on    5:on    6:off
[root@iZ94lm56da9Z ~]# ps -ef |grep crond |grep -v grep
root     27988     1  0 15:30 ?        00:00:00 crond

六、crondtab的總結

  1. 定時任務要加註釋;

  2. 結尾不要有>/dev/null 2>&1;

  3. 如果存在目錄,目錄必須要存在,因爲crontab不會創建新目錄;

  4. 定時任務中的路徑一定要是絕對路徑;

  5. crond服務必須開啓並運行;

    .

    .

    .

    .


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章