簡(jiǎn)介
crontab命令常見(jiàn)于Unix和類(lèi)Unix的操作系統(tǒng)之中,用于設(shè)置周期性被執(zhí)行的指令。該命令從標(biāo)準(zhǔn)輸入設(shè)備讀取指令,并將其存放于“crontab”文件中,以供之后讀取和執(zhí)行。
通常,crontab儲(chǔ)存的指令被守護(hù)進(jìn)程激活,crond常常在后臺(tái)運(yùn)行,每一分鐘檢查是否有預(yù)定的作業(yè)需要執(zhí)行。這類(lèi)作業(yè)一般稱(chēng)為cron jobs。
cron 是 Unix/Linux 中提供定期執(zhí)行 shell 命令的服務(wù),包括 crond 和 crontab 兩部分:
crond: cron 服務(wù)的守護(hù)進(jìn)程,常駐內(nèi)存負(fù)責(zé)定期調(diào)度
crontab: cron 的管理工具,負(fù)責(zé)編輯調(diào)度計(jì)劃
下面的演示在 Ubuntu 16.04 下進(jìn)行。基本的使用方法可以用命令 man crontab 查看
NAME crontab - maintain crontab files for individual users (Vixie Cron)SYNOPSIS crontab [ -u user ] file crontab [ -u user ] [ -i ] { -e | -l | -r }簡(jiǎn)單解釋一下
-e 編輯,類(lèi)似 vim,保存退出時(shí)會(huì)檢查語(yǔ)法
-l 列舉所有任務(wù)
-r 刪除所有任務(wù)
如果 crontab 運(yùn)行出錯(cuò),可以查看日志文件/var/log/syslog
基本語(yǔ)法
cron 的語(yǔ)法非常簡(jiǎn)單,一共分六大塊,其中前五塊用于指定時(shí)間周期,最后一塊是具體執(zhí)行的命令,看起來(lái)大概是這么個(gè)格式:
min hour day month week command
其中
min 表示分鐘,范圍 0-59
hour 表示小時(shí),范圍 0-23
day 表示天,范圍 1-31
可以填寫(xiě) L,表示當(dāng)月最后一天
可以填寫(xiě) W,1W 表示離 1 號(hào)最近的工作日
month 表示月,范圍 1-12
每個(gè)月的最后一天 crontab 本身是不支持的,需要通過(guò)腳本判斷
week 表示周,范圍 0-7
這里 0 和 7 都表示周日
周與日月不能并存,可能會(huì)沖突
可以填寫(xiě) #,4#3 表示當(dāng)月第三個(gè)星期四
可以填寫(xiě) L,5L 表示當(dāng)月最后一個(gè)星期五
command 表示具體要執(zhí)行的命令(最好是絕對(duì)路徑)
如果有多條命令,則需要用&連接,或者將多條命令寫(xiě)在shell腳本中,然后crontab定期執(zhí)行這個(gè)shell腳本即可
另外,類(lèi)似正則表達(dá)式,還有一些特殊符號(hào)幫助我們實(shí)現(xiàn)靈活調(diào)度
* 星號(hào),表示每個(gè)可能的值都接受
例如 * * * * * command 表示每分鐘都執(zhí)行 command 一次
, 逗號(hào),并列時(shí)間
例如 * 6,12,18 * * * command 表示在 6 點(diǎn)、12 點(diǎn)和 18 點(diǎn)執(zhí)行 command 一次
- 減號(hào),連續(xù)區(qū)間
例如 * 9-17 * * * command 表示從 9 點(diǎn)到 17 點(diǎn),每分鐘都執(zhí)行 command 一次
/ 斜線,間隔單位
例如 */5 * * * * command 表示每隔 5 分鐘執(zhí)行 command 一次
系統(tǒng)級(jí) Crontab
如果我們需要執(zhí)行一些權(quán)限較高的指令,就需要利用 root 權(quán)限來(lái)執(zhí)行,這時(shí)的機(jī)制和前面介紹的基本語(yǔ)法也是有區(qū)別的,我們需要編輯的文件是 /etc/crontab。先來(lái)看看其內(nèi)容
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 )#
我們需要在命令和時(shí)間間隔之間添加命令執(zhí)行者,并且也可以添加環(huán)境變量在調(diào)度中使用。我們看到配置文件中有幾個(gè) cron.* 文件,先來(lái)看看還有什么類(lèi)似的文件
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 目錄:該目錄下及子目錄中所有符合調(diào)度語(yǔ)法的文件都會(huì)被執(zhí)行
cron.deny:記錄拒絕執(zhí)行的用戶(hù)
cron.allow:記錄允許執(zhí)行的用戶(hù),這個(gè)文件的優(yōu)先級(jí)較高,一般來(lái)說(shuō)只需要配置一個(gè)文件即可(看是需要白名單還是黑名單機(jī)制)
cron.daily/hourly/monthly/weekly 目錄:里面都是腳本,分別在指定的時(shí)間里執(zhí)行
更多詳細(xì)介紹,可以輸入 man 5 crontab 或 man 8 cron 查閱
原理
為什么我們用 crontab -e 編輯一下就可以添加一個(gè)定時(shí)任務(wù)呢?每次我們添加一行,這個(gè)工作就會(huì)被記錄到 /var/spool/cron/crontab 中,如果我的用戶(hù)名是 dawang,那么對(duì)應(yīng)的文件就是 /var/spool/cron/crontab/dawang(需要 root 權(quán)限才能查看)。不過(guò)不建議直接修改,因?yàn)橹苯有薷氖遣粫?huì)進(jìn)行語(yǔ)法檢查的。
在某些系統(tǒng)中,不一定會(huì)每次都讀取源配置文件(而是利用載入到內(nèi)存的版本),這個(gè)時(shí)候我們就需要重啟 crond 服務(wù),命令為 /sbin/service crond restart
Crond 服務(wù)管理
默認(rèn)情況系統(tǒng)并沒(méi)有為我們啟動(dòng) crond 服務(wù),如果想開(kāi)機(jī)啟動(dòng),需要在 /etc/rc.d/rc.local 中添加 service crond start 這一行,其他的管理命令為
# 啟動(dòng)服務(wù)/sbin/service crond start # 關(guān)閉服務(wù)/sbin/service crond stop # 重啟服務(wù)/sbin/service crond restart # 重新載入配置/sbin/service crond reload
實(shí)例測(cè)試
接著我們來(lái)實(shí)戰(zhàn)一下,第一次使用 crontab -e 需要我們選擇編輯器,默認(rèn)是 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]:
為了驗(yàn)證真的在執(zhí)行,我們建立兩個(gè)每分鐘都執(zhí)行的操作,具體如下(主要關(guān)注最后兩行):
# 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
這里做了兩件事,一個(gè)是每分鐘報(bào)時(shí),另一個(gè)就是每分鐘輸出一段話(huà),這里使用 >> 表示追加輸出,更多輸入輸出方式在下一節(jié)有介紹。如果剛才沒(méi)有啟動(dòng)服務(wù),現(xiàn)在用 service crond start 啟動(dòng),然后等待一段時(shí)間,就可以看到輸出啦,具體參考下面的命令,這里就不贅述了:
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 把標(biāo)準(zhǔn)輸出重定向到文件command >> file 把標(biāo)準(zhǔn)輸出追加到文件command 1 > file 把標(biāo)準(zhǔn)輸出重定向到文件command 2 > file 把標(biāo)準(zhǔn)錯(cuò)誤重定向到文件command 2 >> file 把標(biāo)準(zhǔn)輸出追加到文件command 2>&1 把command命令標(biāo)準(zhǔn)錯(cuò)誤重定向到標(biāo)準(zhǔn)輸出command > file 2>&1 把標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯(cuò)誤一起重定向到文件command >> file 2>&1 把標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯(cuò)誤一起追加到文件command < file 把command命令以file文件作為標(biāo)準(zhǔn)輸入command < file >file2 把command命令以file文件作為標(biāo)準(zhǔn)輸入,以file2文件作為標(biāo)準(zhǔn)輸出command <&- 關(guān)閉標(biāo)準(zhǔn)輸入
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助。
新聞熱點(diǎn)
疑難解答
圖片精選