Difference between revisions of "Cron (简体中文)"

From ArchWiki
Jump to: navigation, search
(范例)
(Asynchronous job processing: Add Fcron.)
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
[[Category:简体中文]]
 
[[Category:简体中文]]
 
[[Category:Daemons and system services (简体中文)]]
 
[[Category:Daemons and system services (简体中文)]]
{{i18n|Cron}}
+
[[de:Cron]]
 +
[[en:Cron]]
 
[[fr:Cron]]
 
[[fr:Cron]]
 +
[[sk:Cron]]
 
{{Article summary start|摘要}}
 
{{Article summary start|摘要}}
 
{{Article summary text|关于 GNU/Linux 系统中的标准计划任务守护进程的概览。}}
 
{{Article summary text|关于 GNU/Linux 系统中的标准计划任务守护进程的概览。}}
Line 196: Line 198:
  
 
==run-parts issue==
 
==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 backup.sh. Since run-parts without options will ignore them. Detail information see man run-parts.
+
cronie使用run-parts来执行在cron.hourly/cron.daily/cron.weekly/cron.monthly里的脚本。
 +
请注意这些文件夹里的脚本名字中不应该含有'.',因为不含有任何参数的run-parts将会忽略他们。例如backup.sh这个脚本是不会被执行的,请将其改名为backup或者其他不含有'.'的名字以定时执行该脚本。
 +
要获取更详细的信息请 man run-parts
  
 
==运行 X 程序==
 
==运行 X 程序==
Line 217: Line 221:
 
  bash -c "xhost +si:localuser:$(whoami)"
 
  bash -c "xhost +si:localuser:$(whoami)"
  
==Asynchronous job processing==
+
== 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):
 
If you regularly turn off your computer but do not want to miss jobs, there are some solutions available (easiest to hardest):
  
; Dcron : Vanilla dcron supports asynchronous job processing. Just put it with @hourly, @daily, @weekly or @monthly with a jobname, like this:
+
===Dcron===
 +
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.
 
  @hourly        ID=greatest_ever_job      echo This job is very useful.
  
; Cronwhip ([http://aur.archlinux.org/packages.php?ID=21079 AUR], [https://bbs.archlinux.org/viewtopic.php?id=57973 forum thread]): Script to automatically run missed cron jobs; works with the default cron implementation, dcron.
+
===Cronwhip===
; Anacron ([http://aur.archlinux.org/packages.php?ID=5196 AUR]): Full replacement for dcron, processes jobs asynchronously.
+
([https://aur.archlinux.org/packages.php?ID=21079 AUR], [https://bbs.archlinux.org/viewtopic.php?id=57973 forum thread]): Script to automatically run missed cron jobs; works with the default cron implementation, dcron.
 +
 
 +
===Anacron===
 +
([https://aur.archlinux.org/packages.php?ID=5196 AUR]): Full replacement for dcron, processes jobs asynchronously.
 +
 
 +
===Fcron===
 +
([https://www.archlinux.org/packages/community/i686/fcron/ Community], [https://bbs.archlinux.org/viewtopic.php?id=140497 forum thread]): Like anacron, fcron assumes the computer is not always running and, unlike anacron, it can schedule events at intervals shorter than a single day.  Like cronwhip, it can run jobs that should have been run during the computer's downtime.
  
 
==参见==
 
==参见==
 
[http://wiki.gotux.net/config:crontab CronTab Config and Examples]
 
[http://wiki.gotux.net/config:crontab CronTab Config and Examples]

Revision as of 04:21, 8 November 2012

摘要 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/i_love_cron.sh

会在夏天(六、七、八月)之外的每周周一到周五的上午9点到下午4点之间每5分钟执行一次 i_love_cron.sh。更多范例和高级配置方法见下文。

基本命令

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!

将会在每个月的每一天的每一个小时的第一分钟(例如,在12:01,1:01,2:01等)执行命令 /bin/echo Hello, world!

类似地,

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

将会在一月的每个工作日每五分钟(例如,在12:00,12:05,12:10等)执行一次相同的命令。

As noted in the Crontab Format section, the line:

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

Will execute the script i_love_cron.sh 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:

/var/spool/cron/root
# root crontab
# DO NOT EDIT THIS FILE MANUALLY! USE crontab -e INSTEAD

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

# SYSTEM DAILY/WEEKLY/... FOLDERS
@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使用run-parts来执行在cron.hourly/cron.daily/cron.weekly/cron.monthly里的脚本。 请注意这些文件夹里的脚本名字中不应该含有'.',因为不含有任何参数的run-parts将会忽略他们。例如backup.sh这个脚本是不会被执行的,请将其改名为backup或者其他不含有'.'的名字以定时执行该脚本。 要获取更详细的信息请 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):

Dcron

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.

Fcron

(Community, forum thread): Like anacron, fcron assumes the computer is not always running and, unlike anacron, it can schedule events at intervals shorter than a single day. Like cronwhip, it can run jobs that should have been run during the computer's downtime.

参见

CronTab Config and Examples