簡介
crontab命令常見于Unix和類Unix的操作系統之中,用于設置周期性被執行的指令。該命令從標準輸入設備讀取指令,并將其存放于“crontab”文件中,以供之后讀取和執行。
通常,crontab儲存的指令被守護進程激活,crond常常在后臺運行,每一分鐘檢查是否有預定的作業需要執行。這類作業一般稱為cron jobs。
cron 是 Unix/Linux 中提供定期執行 shell 命令的服務,包括 crond 和 crontab 兩部分:
crond: cron 服務的守護進程,常駐內存負責定期調度
crontab: cron 的管理工具,負責編輯調度計劃
下面的演示在 Ubuntu 16.04 下進行。基本的使用方法可以用命令 man crontab
查看
NAME crontab - maintain crontab files for individual users (Vixie Cron)SYNOPSIS crontab [ -u user ] file crontab [ -u user ] [ -i ] { -e | -l | -r }
簡單解釋一下
-e
編輯,類似 vim,保存退出時會檢查語法
-l
列舉所有任務
-r
刪除所有任務
如果 crontab 運行出錯,可以查看日志文件/var/log/syslog
基本語法
cron 的語法非常簡單,一共分六大塊,其中前五塊用于指定時間周期,最后一塊是具體執行的命令,看起來大概是這么個格式:
min hour day month week command
其中
min
表示分鐘,范圍 0-59
hour
表示小時,范圍 0-23
day
表示天,范圍 1-31
可以填寫 L,表示當月最后一天
可以填寫 W,1W 表示離 1 號最近的工作日
month
表示月,范圍 1-12
每個月的最后一天 crontab 本身是不支持的,需要通過腳本判斷
week
表示周,范圍 0-7
這里 0 和 7 都表示周日
周與日月不能并存,可能會沖突
可以填寫 #,4#3 表示當月第三個星期四
可以填寫 L,5L 表示當月最后一個星期五
command
表示具體要執行的命令(最好是絕對路徑)
如果有多條命令,則需要用&連接,或者將多條命令寫在shell腳本中,然后crontab定期執行這個shell腳本即可
另外,類似正則表達式,還有一些特殊符號幫助我們實現靈活調度
*
星號,表示每個可能的值都接受
例如 * * * * * command 表示每分鐘都執行 command 一次
,
逗號,并列時間
例如 * 6,12,18 * * * command 表示在 6 點、12 點和 18 點執行 command 一次
-
減號,連續區間
例如 * 9-17 * * * command 表示從 9 點到 17 點,每分鐘都執行 command 一次
/
斜線,間隔單位
例如 */5 * * * * command 表示每隔 5 分鐘執行 command 一次
系統級 Crontab
如果我們需要執行一些權限較高的指令,就需要利用 root 權限來執行,這時的機制和前面介紹的基本語法也是有區別的,我們需要編輯的文件是 /etc/crontab。先來看看其內容
dawang@dawang-Parallels-Virtual-Platform:~$ cat /etc/crontab# /etc/crontab: system-wide crontab# Unlike any other crontab you don't have to run the `crontab'# command to install the new version when you edit this file# and files in /etc/cron.d. These files also have username fields,# that none of the other crontabs do.SHELL=/bin/shPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin# m h dom mon dow user command17 * * * * root cd / && run-parts --report /etc/cron.hourly25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )#
我們需要在命令和時間間隔之間添加命令執行者,并且也可以添加環境變量在調度中使用。我們看到配置文件中有幾個 cron.* 文件,先來看看還有什么類似的文件
dawang@dawang-Parallels-Virtual-Platform:~$ ll /etc | grep cron-rw-r--r-- 1 root root 401 12月 29 2014 anacrontabdrwxr-xr-x 2 root root 4096 4月 21 06:14 cron.d/drwxr-xr-x 2 root root 4096 4月 21 06:14 cron.daily/drwxr-xr-x 2 root root 4096 4月 21 06:08 cron.hourly/drwxr-xr-x 2 root root 4096 4月 21 06:14 cron.monthly/-rw-r--r-- 1 root root 722 4月 6 05:59 crontabdrwxr-xr-x 2 root root 4096 4月 21 06:14 cron.weekly/
其中
cron.d
目錄:該目錄下及子目錄中所有符合調度語法的文件都會被執行
cron.deny
:記錄拒絕執行的用戶
cron.allow
:記錄允許執行的用戶,這個文件的優先級較高,一般來說只需要配置一個文件即可(看是需要白名單還是黑名單機制)
cron.daily/hourly/monthly/weekly
目錄:里面都是腳本,分別在指定的時間里執行
更多詳細介紹,可以輸入 man 5 crontab
或 man 8 cron
查閱
原理
為什么我們用 crontab -e
編輯一下就可以添加一個定時任務呢?每次我們添加一行,這個工作就會被記錄到 /var/spool/cron/crontab 中,如果我的用戶名是 dawang,那么對應的文件就是 /var/spool/cron/crontab/dawang(需要 root 權限才能查看)。不過不建議直接修改,因為直接修改是不會進行語法檢查的。
在某些系統中,不一定會每次都讀取源配置文件(而是利用載入到內存的版本),這個時候我們就需要重啟 crond 服務,命令為 /sbin/service crond restart
Crond 服務管理
默認情況系統并沒有為我們啟動 crond 服務,如果想開機啟動,需要在 /etc/rc.d/rc.local 中添加 service crond start 這一行,其他的管理命令為
# 啟動服務/sbin/service crond start # 關閉服務/sbin/service crond stop # 重啟服務/sbin/service crond restart # 重新載入配置/sbin/service crond reload
實例測試
接著我們來實戰一下,第一次使用 crontab -e
需要我們選擇編輯器,默認是 nano,但是我選擇了 vim
dawang@dawang-Parallels-Virtual-Platform:~$ crontab -eno crontab for dawang - using an empty oneSelect an editor. To change later, run 'select-editor'. 1. /bin/ed 2. /bin/nano <---- easiest 3. /usr/bin/vim.tinyChoose 1-3 [2]:
為了驗證真的在執行,我們建立兩個每分鐘都執行的操作,具體如下(主要關注最后兩行):
# Edit this file to introduce tasks to be run by cron.## Each task to run has to be defined through a single line# indicating with different fields when the task will be run# and what command to run for the task## To define the time you can provide concrete values for# minute (m), hour (h), day of month (dom), month (mon),# and day of week (dow) or use '*' in these fields (for 'any').## Notice that tasks will be started based on the cron's system# daemon's notion of time and timezones.## Output of the crontab jobs (including errors) is sent through# email to the user the crontab file belongs to (unless redirected).## For example, you can run a backup of all your user accounts# at 5 a.m every week with:# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/## For more information see the manual pages of crontab(5) and cron(8)## m h dom mon dow command* * * * * date >> /home/dawang/date.txt* * * * * echo "time to go!" >> /home/dawang/time.txt
這里做了兩件事,一個是每分鐘報時,另一個就是每分鐘輸出一段話,這里使用 >> 表示追加輸出,更多輸入輸出方式在下一節有介紹。如果剛才沒有啟動服務,現在用 service crond start
啟動,然后等待一段時間,就可以看到輸出啦,具體參考下面的命令,這里就不贅述了:
dawang@dawang-Parallels-Virtual-Platform:~$ ll | grep txt-rw-rw-r-- 1 dawang dawang 1849 7月 26 16:08 date.txt-rw-rw-r-- 1 dawang dawang 516 7月 26 16:08 time.txtdawang@dawang-Parallels-Virtual-Platform:~$ tail -n 10 date.txt2016年 07月 26日 星期二 16:01:01 CST2016年 07月 26日 星期二 16:02:01 CST2016年 07月 26日 星期二 16:03:01 CST2016年 07月 26日 星期二 16:04:01 CST2016年 07月 26日 星期二 16:05:01 CST2016年 07月 26日 星期二 16:06:01 CST2016年 07月 26日 星期二 16:07:01 CST2016年 07月 26日 星期二 16:08:01 CST2016年 07月 26日 星期二 16:09:01 CST2016年 07月 26日 星期二 16:10:01 CSTdawang@dawang-Parallels-Virtual-Platform:~$ tail -n 10 time.txt time to go!time to go!time to go!time to go!time to go!time to go!time to go!time to go!time to go!time to go!
重定向命令
這里直接給出例子
command > file 把標準輸出重定向到文件command >> file 把標準輸出追加到文件command 1 > file 把標準輸出重定向到文件command 2 > file 把標準錯誤重定向到文件command 2 >> file 把標準輸出追加到文件command 2>&1 把command命令標準錯誤重定向到標準輸出command > file 2>&1 把標準輸出和標準錯誤一起重定向到文件command >> file 2>&1 把標準輸出和標準錯誤一起追加到文件command < file 把command命令以file文件作為標準輸入command < file >file2 把command命令以file文件作為標準輸入,以file2文件作為標準輸出command <&- 關閉標準輸入
總結
以上就是這篇文章的全部內容了,希望對大家的學習或者工作能帶來一定的幫助。
新聞熱點
疑難解答