Linux系統管理(一):定時任務Crontab命令

前言:


我們知道,at命令是用戶直接給定一個時間點去執行某特定的任務,對於一些日常都需要去執行的命令,我們不能每天都去執行一次,所以,Linux提供了一個循環運行的例行工作命令“crontab”,它是由cron(crond)這個系統服務去控制的,IT運維人員通過配置crontab配置文件去實現循環運行,目前它是互聯網很常用的技術。(我們也可以把它理解爲Windows下的“任務計劃程序”),接下來,我們看一下怎麼去使用這個命令:


Crontab的語法

[root@localhost ~]# crontab [-u username] [-l|-e|-r]
選項與參數:
-u  : 只有 root 才能進行這個任務,幫其他使用者創建/移除 crontab 工作任務
-e  : 編輯 crontab 的任務
-l  : 查看 crontab 的任務
-r  : 移除所有的 crontab 的任務,若僅要移除一項,請用 -e 去編輯。
例如:每兩分鐘往/tmp/output.txt文件寫入”This is only for crontab test”
[root@localhost ~]# crontab -e
*/2 * * * * echo "This is only for crontab test" >> /tmp/output.txt
執行結果
[root@localhost ~]# ls -lh /tmp/output.txt
-rw-r--r--. 1 root root 30 Jan 14 17:46 /tmp/output.txt
[root@localhost ~]# ls -lh /tmp/output.txt
-rw-r--r--. 1 root root 60 Jan 14 17:48 /tmp/output.txt
[root@localhost ~]# cat /tmp/output.txt
This is only for crontab test
This is only for crontab test
執行日誌查詢: 日誌的存儲位置:/var/log/cron
[root@localhost log]# cat cron | tail 3
Jan 14 17:46:01 localhost CROND[4975]: (root) CMD (echo "This is only for crontab test" >> /tmp/output.txt )
Jan 14 17:48:02 localhost CROND[5023]: (root) CMD (echo "This is only for crontab test" >> /tmp/output.txt )
Jan 14 17:50:01 localhost CROND[5070]: (root) CMD (echo "This is only for crontab test" >>

一些特殊字符的使用

逗號(,)
例子:每天的下午6點與8點備份/etc/目錄至/backup/下
      0 6,8 * * * cp /etc /backup/
減號(-)
例子:每天的9點至12的每小時的30分鐘備份/etc/datamakr至/backup/log/
      30 9-12 * * * cp /etc/datamakr /backup/log/

斜線(\n)
例子:每分鐘,如上面的例子*/2每兩分鐘執行一次命令

Crontab命令的使用限制


/etc/cron.allow:將可以使用 crontab 的帳號寫入其中,若不在這個文件內的使用者則不可使用 crontab;

/etc/cron.deny:將不可以使用 crontab 的帳號寫入其中,若未記錄到這個文件當中的使用者,就可以使用 crontab 。

以優先順序來說, /etc/cron.allow 比 /etc/cron.deny 要優先, 而判斷上面,這兩個文件只選擇一個來限制而已,因此,建議你只要保留一個即可, 免得影響自己在配置上面的判斷!一般來說,系統默認是保留 /etc/cron.deny ,你可以將不想讓他運行 crontab 的那個使用者寫入 /etc/cron.deny 當中,一個帳號一行!該命令從標準輸入設備讀取指令,並將其存放於“crontab”文件中,以供之後讀取和執行。


Crontab配置文件的詳解


首先我們來看看Crontab配置文件內容

[root@localhost ~]# cat /etc/crontab
SHELL=/bin/bash    #使用的shell文件
PATH=/sbin:/bin:/usr/sbin:/usr/bin   #命令的搜索路徑
MAILTO=root   #將結果以郵件的方式轉給給誰,默認是管理員root(也可以是輸入郵箱地址)
# 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

備註:爲什麼我們上面利用“crontab -e”執行的命令沒有保存在這個文件呢?原因是“crontab -e”是針對使用者的cron來設計的,如果您想要一些日常需能夠循環運行,那麼直接編輯“/etc/crontab”這個文件,cron會每隔分鐘的去讀取一次/etc/crontab 與 /var/spool/cron 裏面的數據內容,所以,只要你編輯完 /etc/crontab 這個文件,並且將他儲存之後,那麼 cron 的配置就自動的會來運行了。


選項Minuteshourday-of-monthmonth of yearday of weekCommand
意義分鐘小時日期月份 命令
取值範圍0-590-231-311-120-6,0,7代表週日


Cron服務進程的狀態查看

CentOS 7: Systemctl status crond
CentOS 6: service crond status

重啓

/etc/init.d/crond restart


參考文獻:

http://vbird.dic.ksu.edu.tw/linux_basic/0430cron_3.php

http://www.php-note.com/article/detail/786


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