For faster navigation, this Iframe is preloading the Wikiwand page for Cron.

Cron

本条目存在以下问题,请协助改善本条目或在讨论页针对议题发表看法。 此条目包含指南或教学内容。 (2024年1月23日)请借由移除或重写指南段落来改善条目,或在讨论页提出讨论。 此条目可参照英语维基百科相应条目来扩充。 (2024年1月23日)若您熟悉来源语言和主题,请协助参考外语维基百科扩充条目。请勿直接提交机械翻译,也不要翻译不可靠、低品质内容。依版权协议,译文需在编辑摘要注明来源,或于讨论页顶部标记((Translated page))标签。

工具型软件cron是一款类Unix操作系统下的基于时间的任务管理系统。用户们可以通过cron在固定时间、日期、间隔下,运行定期任务(可以是命令和脚本)。cron常用于运维和管理,但也可用于其他地方,如:定期下载文件和邮件。cron该词来源于希腊语chronos(χρόνος),原意是时间。

通常,任务时间表(crontab)文件储存的指令被crond守护进程激活,守护进程在后台运行,并每一分钟检查是否有定期的作业需要执行。这类作业一般称为cron jobs


举例

  1. ┌───────────── minute (0–59)
  2. │ ┌───────────── hour (0–23)
  3. │ │ ┌───────────── day of the month (1–31)
  4. │ │ │ ┌───────────── month (1–12)
  5. │ │ │ │ ┌───────────── day of the week (0–6) (Sunday to Saturday;
  6. │ │ │ │ │ 7 is also Sunday on some systems)
  7. │ │ │ │ │
  8. │ │ │ │ │
  9. * * * * * <command to execute>

crond守护进程

[编辑]

crond是一个用于执行周期命令的守护进程。[1]通过守护进程(/lib/systemd/system/crond.service )形式运行的cron程序称为crond。[2] cron会通过以下路径查找crontabs:[3]

  • /etc/crontab:为系统任务时间表(crontab)以前用于跑以日为单位、以周为单位、以月为单位的任务,现在用于跑anacron。
  • /etc/cron.d/:该目录包含系统层次的任务时间表(crontabs)[4],不同用户共同使用。
  • /var/spool/cron/:该路径包含用户通过crontab 命令创建的任务时间表(crontables)。

crontab命令

[编辑]

crontab 命令用于维护每个用户的任务时间表(crontab)文件。[5]

crontab 命令用于安装任务时间表(crontab)文件,删除和列举 crond守护进程已经在使用的任务时间表(crontab)。每个用户都会拥有自己独自的任务时间表(crontab),这些文件会存放在/var/spool/目录下,不建议用户直接修改这些文件。MLS模式下的SELinux,你可以为这些任务时间表(crontab)分等级。[6]

crontab文件

[编辑]

crontab文件包含crond守护进程所需的一系列作业和指令。[7]

crontab文件的每一行均遵守特定的格式,由空格或tab分隔为数个领域,每个领域可以放置单一或多个数值。

文件格式

[编辑]

用户文件

[编辑]

/var/spool/cron/下的任务时间表(crontab)文件是用户层次的,格式如下:

# 文件格式說明
# ┌──分鐘(0 - 59)
# │ ┌──小時(0 - 23)
# │ │ ┌──日(1 - 31)
# │ │ │ ┌─月(1 - 12)
# │ │ │ │ ┌─星期(0 - 6,表示从周日到周六)
# │ │ │ │ │
# *  *  *  *  * 被執行的命令

注:

  1. 在某些系统里,星期日也可以为7
  2. 不很直观的用法:如果日期和星期同时被设置,那么其中的一个条件被满足时,指令便会被执行。请参考下例。
  3. 前5个域称之分时日月周,可方便个人记忆。

从第六个域起,指明要执行的命令。

系统文件

[编辑]

/etc/crontab/etc/cron.d/目录下的任务时间表(crontabs)文件是系统层次的,格式如下:

# 文件格式說明
# ┌──分鐘(0 - 59)
# │ ┌──小時(0 - 23)
# │ │ ┌──日(1 - 31)
# │ │ │ ┌─月(1 - 12)
# │ │ │ │ ┌─星期(0 - 6,表示从周日到周六)
# │ │ │ │ │
# *  *  *  *  *  用户名 被執行的命令

系统层次的任务时间表(crontabs)的任务经常会指定一个或以上的用户进行执行,因此任务时间表(crontabs)文件需要增加“用户名”字段。[8]

表达式

[编辑]

在一个区域里填写多个数值的方法:

  • 逗号(,)表示列举,例如: 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"。

非标准字符

[编辑]

某些cron程序的扩展版本(如:Quartz Java scheduler)也支持斜线('/')操作符,用于表示跳过某些给定的数。例如,“*/3”在小时域中等于“0,3,6,9,12,15,18,21”等被3整除的数;

例子

[编辑]

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

初看似要在四月的第一个星期日早晨1时59分运行shift_my_times.sh,但是这样设置不对。

特殊地,当星期域(第五个域)被指定为 * 时,星期域与日域(第三个域)执行“与”操作;而当星期域(第五个域)被指定为 * 以外的内容时,星期域与日域执行“或”操作。 所以这个程序会在4月1日至7日以及4月余下的每一个星期日执行。

另一个常见错误是对分钟设置的误用。下例欲一个程序两个小时运行一次:

# 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

不发送电邮

[编辑]

如果输出结果来自crontab里的命令,那么cron守护进程会用电邮将它发给用户。

  • 若想关闭某个命令的输出结果,可以将输出结果重定向至/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

其他话题

[编辑]

参考文献

[编辑]
  1. ^ daemon to execute scheduled commands. (原始内容存档于2019-12-18). 
  2. ^ DESCRIPTION of cron. (原始内容存档于2019-12-18). 
  3. ^ Cron checks these files and directories:. (原始内容存档于2019-12-18). 
  4. ^ /etc/cron.d/ a directory for storing system crontabs.. (原始内容存档于2019-12-08). 
  5. ^ crontab命令的名字章节. (原始内容存档于2020-02-03). 
  6. ^ crontab命令的描述. (原始内容存档于2020-02-03). 
  7. ^ A crontab file contains instructions for the cron(8) daemon. (原始内容存档于2019-12-08). 
  8. ^ Jobs in /etc/cron.d/. (原始内容存档于2019-12-08). 

外部链接

[编辑]

文档

[编辑]

软件

[编辑]
{{bottomLinkPreText}} {{bottomLinkText}}
Cron
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?