cron 是一個(gè) Linux 下的定時(shí)執(zhí)行工具,可以在無需人工干預(yù)的情況下運(yùn)行作業(yè)。守護(hù)進(jìn)程 cron 會(huì)讀取 crontab 文件,根據(jù)配置在指定的時(shí)間執(zhí)行任務(wù)。contab 命令用來添加、刪除、顯示 cron 任務(wù)表。
(Ubuntu 環(huán)境)可以通過 service 命令啟停 cron 服務(wù):service cron status # 查看 cron 服務(wù)狀態(tài)service cron start # 啟動(dòng) cron 服務(wù)service cron stop # 停止 cron 服務(wù)service cron restart # 重啟 cron 服務(wù)service cron reload # 重新加載 cron 服務(wù)配置
crontab 命令命令格式crontab [-u user] filecrontab [-u user] [-l | -r | -e] [-i] [-s]
命令參數(shù)-u 指定用戶
-l 顯示當(dāng)前的 crontab 文件內(nèi)容。
-r 刪除當(dāng)前的 crontab 文件內(nèi)容。
-e 編輯當(dāng)前的 crontab 文件內(nèi)容,如果文件不存在則創(chuàng)建新文件。
-i 在刪除 crontab 文件內(nèi)容時(shí)予以提示。
crontab 文件格式:minute hour day_of_month month day_of_week command
字段含義
| 字段 | 含義 |
| minute | 分鐘,取值范圍是 (0 - 59) |
| hour | 小時(shí),取值范圍是 (0 - 23) |
| day_of_month | 日期,取值范圍是 (0 - 31) |
| month | 月份,取值范圍是 (1 - 12),也可以用 jan,feb,mar,aPR... 表示 |
| day_of_week | 星期,取值范圍是 (0 - 6),其中周日用 0 或 7 表示,也可以用 sun,mon,tue,wed,thu,fri,sat 表示 |
| command | 要執(zhí)行的命令 |
特殊符號(hào)含義
| 特殊符號(hào) | 說明 |
| * | 通配所有可能的取值 |
| , | 指定取值列表,例如 1,3,5,7,9 |
| - | 指定整數(shù)范圍,例如 1-5 |
| / | 指定間隔頻率 |
cron 表達(dá)式例子
| 示例 | 說明 |
| */15 * * * * | 每隔 15 分鐘執(zhí)行任務(wù) |
| 0 */2 * * * | 每隔 2 個(gè)小時(shí),在整點(diǎn)的時(shí)候執(zhí)行任務(wù) |
| 0 3 * * * * | 每天凌晨三點(diǎn)的時(shí)候執(zhí)行任務(wù) |
| 0 0 5,15,25 * * | 每月 5 號(hào),15 號(hào),25 號(hào)凌晨的時(shí)候執(zhí)行任務(wù) |
| 15 12 * * 1 | 每周一中午 12 點(diǎn) 15 分的時(shí)候執(zhí)行任務(wù) |
| 0,30 12-20 * * * | 每天 12:00 至 20:59 之間,在整點(diǎn)或半點(diǎn)的時(shí)候執(zhí)行任務(wù) |
a) 添加 cron 任務(wù)
huey@huey-K42JE:~/huey/linux/cmdline$ echo "*/3 * * * * date >> ~/huey/linux/cmdline/cron.out" > mycronhuey@huey-K42JE:~/huey/linux/cmdline$ crontab mycron
b) 刪除 cron 任務(wù)
huey@huey-K42JE:~/huey/linux/cmdline$ crontab -r
c) 顯示 cron 任務(wù)
huey@huey-K42JE:~/huey/linux/cmdline$ crontab -l*/3 * * * * date >> ~/huey/linux/cmdline/cron.out
新聞熱點(diǎn)
疑難解答
圖片精選