Cron
![本页使用了标题或全文手工转换](https://upload.wikimedia.org/wikipedia/commons/thumb/c/cd/Zh_conversion_icon_m.svg/35px-Zh_conversion_icon_m.svg.png)
![]() |
举例
- ┌───────────── minute (0–59)
- │ ┌───────────── hour (0–23)
- │ │ ┌───────────── day of the month (1–31)
- │ │ │ ┌───────────── month (1–12)
- │ │ │ │ ┌───────────── day of the week (0–6) (Sunday to Saturday;
- │ │ │ │ │ 7 is also Sunday on some systems)
- │ │ │ │ │
- │ │ │ │ │
- * * * * * <command to execute>
crond守 护进程
[编辑]crond/lib/systemd/system/crond.service
)
/etc/crontab
:为系统任 务时间表(crontab)。以前 用 于跑以日为单位 、以周为单位 、以月为单位 的 任 务,现在用 于跑anacron。/etc/cron.d/
:该目录包含 系 统层次 的 任 务时间表(crontabs)[4],不同 用 户共同 使用 。/var/spool/cron/
:该路径 包含 用 户通过crontab
命令 创建的 任 务时间表(crontables)。
crontab命令
[编辑]crontab
crontab
/var/spool/
crontab文 件
[编辑]crontab
crontab
文 件 格式
[编辑]用 户文件
[编辑]/var/spool/cron/
#文 件 格式 說明 # ┌──分 鐘 (0 - 59) # │ ┌──小 時 (0 - 23) # │ │ ┌──日 (1 - 31) # │ │ │ ┌─月 (1 - 12) # │ │ │ │ ┌─星 期 (0 - 6,表示 从周日 到 周 六 ) # │ │ │ │ │ # * * * * *被 執行 的 命令
在 某 些系统里,星 期日 也可以为7不 很直觀 的 用法 :如果日 期 和 星 期 同時 被 設定 ,那 麼其中 的 一 個 條件 被 滿足 時 ,指令 便 會 被 執行 。請参考 下 例 。前 5個 域 稱 之 分 時日 月 周 ,可 方便 個人 記憶 。
系 统文件
[编辑]/etc/crontab
/etc/cron.d/
# 文 件 格式 說明
# ┌──分 鐘 (0 - 59)
# │ ┌──小 時 (0 - 23)
# │ │ ┌──日 (1 - 31)
# │ │ │ ┌─月 (1 - 12)
# │ │ │ │ ┌─星 期 (0 - 6,表示 从周日 到 周 六 )
# │ │ │ │ │
# * * * * * 用 户名 被 執行 的 命令
表 达式
[编辑]- 逗号(
,
)表示 列 举,例 如:1,3,4,7 * * * * echo hello world
表示 ,在 每 小 时的1、3、4、7分 时,打 印 "hello world"。 - 连词
符 (-
)表示 范围,例 如:1-6 * * * * echo hello world
,表示 ,每 小 时的1到 6分 钟内,每 分 钟都会 打 印 "hello world"。 星 号 (*
)代表 任 何 可能 的 值。例 如:在 “小 时域”里 的 星 号 等 于是“每 一 个小时”。百 分 号 (%
)表示 “每 "。例 如:*%10 * * * * echo hello world
表示 ,每 10分 钟打印 一 回 "hello world"。
非 标准字 符
[编辑]
例 子
[编辑]AIX系 统管理 员的Crontab文 件
[编辑]#================================================================= # SYSTEM ACTIVITY REPORTS # 8am-5pm activity reports every 20 mins during weekdays. # activity reports every hour on Saturday and Sunday. # 6pm-7am activity reports every hour during weekdays. # summary prepared at 18:05 every weekday. #================================================================= 0,20,40 8-17 * * 1-5 /usr/lib/sa/sa1 1200 3 & 0 * * * 0,6 /usr/lib/sa/sa1 & 0 18-7 * * 1-5 /usr/lib/sa/sa1 & 5 18 * * 1-5 /usr/lib/sa/sa2 -s 8:00 -e 18:01 -i 3600 -ubcwyaqvm &
常見 錯誤
[编辑]#錯誤 的 例 子 : 1 2 3 4 5 touch ~/error_`date "+%Y%m%d"`.txt
/bin/sh: unexpected EOF while looking for `'''''''
#正確 的 例 子 : 1 2 3 4 5 touch ~/right_$(date +\%Y\%m\%d).txt
#使用 單 引號也可以解决問題 : 1 2 3 4 5 touch ~/error_$(date '+%Y%m%d').txt
#使用 單 引號就不用 加 反 斜線 了 。這個例 子 會 產 生 這樣一 個 文 件 ~/error_\2006\04\03.txt 1 2 3 4 5 touch ~/error_$(date '+\%Y\%m\%d').txt
# Prepare for the daylight savings time shift 59 1 1-7 4 0 /root/shift_my_times.sh
另一個常見錯誤是對分鐘設置的誤用。
# adds date to a log file * 0,2,4,6,8,10,12,14,16,18,20,22 * * * date >> /var/log/date.log
而
# runs the date command every even hour at the top of the hour 0 0,2,4,6,8,10,12,14,16,18,20,22 * * * date >> /var/log/date.log
# an even better way 0 */2 * * * date >> /var/log/date.log
不 发送电子邮件
[编辑]如果输出结果
若 想 关闭某 个命令 的 输出结果,可 以将输出结果重定 向 至 /dev/null
。
>/dev/null 2>&1
在 常用 的 Vixie cron中 ,也可以在文 件 的 开始部分 加入 命令 来 关闭所有 命令 的 邮件输出:
MAILTO=""
cron命令
[编辑]/etc/init.d/cron reload
#Reloading configuration files for periodic command scheduler: cron.
/etc/init.d/cron stop
#Stopping cron (via systemctl): cron.service.
/etc/init.d/cron start
#Starting cron (via systemctl): cron.service.
/etc/init.d/cron restart
#Restarting cron (via systemctl): cron.service.
/etc/init.d/cron status
其他话题
[编辑]- At:
在 未来 某 个时间运行 一 个作业。 - Anacron:runs job on a periodic interval, anachronistically.
- Launchd:Mac OS X/Darwin cron
替 代 版本 。 - Unix
程 序列 表
参考 文献
[编辑]- ^ daemon to execute scheduled commands. (
原始 内容 存 档于2019-12-18). - ^ DESCRIPTION of cron. (
原始 内容 存 档于2019-12-18). - ^ Cron checks these files and directories:. (
原始 内容 存 档于2019-12-18). - ^ /etc/cron.d/ a directory for storing system crontabs.. (
原始 内容 存 档于2019-12-08). - ^ crontab
命令 的 名字 章 节. (原始 内容 存 档于2020-02-03). - ^ crontab
命令 的 描述. (原始 内容 存 档于2020-02-03). - ^ A crontab file contains instructions for the cron(8) daemon. (
原始 内容 存 档于2019-12-08). - ^ Jobs in /etc/cron.d/. (
原始 内容 存 档于2019-12-08).
外部 链接
[编辑]文 档
[编辑]- Crontab : Scheduling Tasks(页面
存 档备份,存 于互联网档案 馆) - Computer Hope(页面
存 档备份,存 于互联网档案 馆) Linux / UNIX中 crontab的 用法 - Opengroup's crontab specification(页面
存 档备份,存 于互联网档案 馆) - UNIX 03正式 文 档 - Crontab - Reference and Examples at mkaz.com(页面
存 档备份,存 于互联网档案 馆)
软件
[编辑]- Cron for Windows
- CVSweb for FreeBSD's cron - Paul Vixie
的 1993 Vixie cron 3.0版 本 修 补了一 些错误 - fcron(页面
存 档备份,存 于互联网档案 馆) - vixiecron / anacron的 增强 版本 (GPL)
|