Cron (简体中文)

From ArchWiki
Revision as of 13:47, 29 February 2012 by Skydiver (Talk | contribs) (Article created)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.

Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어

External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

摘要 help replacing me
关于 GNU/Linux 系统中的标准计划任务守护进程的概览。
Gentoo Linux Cron 指南

Cron 是一个强大的任务计划程序,支持 GNU/Linux 和许多其他的操作系统。它可以通过在给定时间运行命令来自动化周期性的任务。它有多个可用的应用程序;大多数简单的重复的任务,从备份到收取邮件,都可以用 cron 自动化,节省用户时间和头疼。


有很多 cron 的实现,用户可以选择其一。cronie 可以从 [core] 中获得,并且它被作为 base 软件组的一部分安装,你可以检查它是否正确安装:

# pacman -S --needed cronie

截至2011年5月,Arch Linux 默认的 cron 实现是 dcron (Dillon's Cron),它仍然被支持,可以从 [extra] 找到:

# pacman -S dcron

另外,用户可以安装 fcron (来自 [community]) 或者 bcronAUR 或者 vixie-cronAUR (来自 AUR); 它们都提供了广泛的功能和配置选项。

Gentoo Linux Cron 指南 提供了一个这些实现之间的比较。


注意: 本部分内容已过期,暂时不翻译。

Crontab 格式

crontab 的基本格式是:

<分钟> <小时> <日> <月份> <星期> <命令>
  • 分钟 值从 0 到 59.
  • 小时 值从 0 到 23.
  • 值从 1 到 31.
  • 值从 1 到 12.
  • 星期 值从 0 到 6, 0 代表星期日.


*0,*5 9-16 * 1-5,9-12 1-5 /home/user/bin/



Crontabs 不应该直接编辑;用户应该使用 crontab 程序来处理他们的 crontabs。为了能够访问这个命令,用户必须添加到 users 用户组 (见 gpasswd 命令).

要查看 crontabs,用户应该运行下面的命令:

$ crontab -l

要编辑 crontabs,可以使用:

$ crontab -e

要移除 crontabs, 可以使用:

$ crontab -d

如果用户有一个保存好的 crontab 想要用它完全覆盖旧的 crontab,可以使用:

$ crontab saved_crontab_filename

想从命令行(Wikipedia:stdin)覆盖一个 crontab,使用:

$ crontab - 

想编辑别的用户的 crontab, 使用root运行下面的命令:

# crontab -u username -e

同一个格式 (追加 "-u username" 到命令后) 也可以用来列出或删除 crontabs。

如果想使用 nano 而不是 vi 作为 crontab 编辑器,添加下面的变量到 /etc/bash.bashrc:

export EDITOR="/usr/bin/nano"




01 * * * * /bin/echo Hello, world!

runs the command /bin/echo Hello, world! on the first minute of every hour of every day of every month (i.e. at 12:01, 1:01, 2:01, etc.)


*/5 * * jan mon-fri /bin/echo Hello, world!

runs the same job every five minutes on weekdays during the month of January (i.e. at 12:00, 12:05, 12:10, etc.)

As noted in the Crontab Format section, the line:

*0,*5 9-16 * 1-5,9-12 1-5 /home/user/bin/

Will execute the script at five minute intervals from 9 AM to 5 PM (excluding 5 PM itself) every weekday (Mon-Fri) of every month except during the summer (June, July, and August).


The cron daemon parses a configuration file known as crontab. Each user on the system can maintain a separate crontab file to schedule commands individually. The root user's crontab is used to schedule system-wide tasks (though users may opt to use /etc/crontab or the /etc/cron.d directory, depending on which cron implementation they choose).

There are slight differences between the crontab formats of the different cron daemons. The default root crontab for dcron looks like this:

# root crontab

# man 1 crontab for acceptable formats:
#    <minute> <hour> <day> <month> <dow> <tags and command>
#    <@freq> <tags and command>

@hourly         ID=sys-hourly   /usr/sbin/run-cron /etc/cron.hourly
@daily          ID=sys-daily    /usr/sbin/run-cron /etc/cron.daily
@weekly         ID=sys-weekly   /usr/sbin/run-cron /etc/cron.weekly
@monthly        ID=sys-monthly  /usr/sbin/run-cron /etc/cron.monthly

These lines exemplify one of the formats that crontab entries can have, namely whitespace-separated fields specifying:

  1. @period
  2. ID=jobname (this tag is specific to dcron)
  3. command

The other standard format for crontab entries is:

  1. minute
  2. hour
  3. day
  4. month
  5. day of week
  6. command

The crontab files themselves are usually stored as /var/spool/cron/username. For example, root's crontab is found at /var/spool/cron/root

See the crontab man page for further information and configuration examples.

run-parts issue

cronie use run-parts to carry out script in cron.daily/cron.week.y/cron.montly. Be carefull that the script name in these folders should not have any '.', like Since run-parts without options will ignore them. Detail information see man run-parts.

运行 X 程序

If you find that you cannot run X apps from cron jobs then put this before the command:

export DISPLAY=:0.0 ;

That sets the DISPLAY variable to the first display; which is usually right unless you like to run multiple xservers on your machine.

If it still does not work then you need to use xhost to give your user control over X11:

# xhost +si:localuser:$(whoami)

I put it in my gnome `Startup Applications' like this:

bash -c "xhost +si:localuser:$(whoami)"

Asynchronous job processing

If you regularly turn off your computer but do not want to miss jobs, there are some solutions available (easiest to hardest):

Vanilla dcron supports asynchronous job processing. Just put it with @hourly, @daily, @weekly or @monthly with a jobname, like this:
@hourly         ID=greatest_ever_job      echo This job is very useful.
Cronwhip (AUR, forum thread)
Script to automatically run missed cron jobs; works with the default cron implementation, dcron.
Anacron (AUR)
Full replacement for dcron, processes jobs asynchronously.


CronTab Config and Examples