前言:php
咱們知道,at命令是用戶直接給定一個時間點去執行某特定的任務,對於一些平常都須要去執行的命令,咱們不能天天都去執行一次,因此,Linux提供了一個循環運行的例行工做命令「crontab」,它是由cron(crond)這個系統服務去控制的,IT運維人員經過配置crontab配置文件去實現循環運行,目前它是互聯網很經常使用的技術。(咱們也能夠把它理解爲Windows下的「任務計劃程序」),接下來,咱們看一下怎麼去使用這個命令:linux
Crontab的語法shell
[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" >>
一些特殊字符的使用
bash
逗號(,) 例子:天天的下午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;ide
/etc/cron.deny:將不可使用 crontab 的賬號寫入其中,若未記錄到這個文件當中的使用者,就可使用 crontab 。spa
以優先順序來講, /etc/cron.allow 比 /etc/cron.deny 要優先, 而判斷上面,這兩個文件只選擇一個來限制而已,所以,建議你只要保留一個便可, 省得影響本身在配置上面的判斷!通常來講,系統默認是保留 /etc/cron.deny ,你能夠將不想讓他運行 crontab 的那個使用者寫入 /etc/cron.deny 當中,一個賬號一行!該命令從標準輸入設備讀取指令,並將其存放於「crontab」文件中,以供以後讀取和執行。設計
Crontab配置文件的詳解
rest
首先咱們來看看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 的配置就自動的會來運行了。
選項 | Minutes | hour | day-of-month | month of year | day of week | Command |
意義 | 分鐘 | 小時 | 日期 | 月份 | 周 | 命令 |
取值範圍 | 0-59 | 0-23 | 1-31 | 1-12 | 0-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