[wiki]CronHowto

参与到Ubuntu的翻译中来
头像
oneleaf
论坛管理员
帖子: 10441
注册时间: 2005-03-27 0:06
系统: Ubuntu 12.04

[wiki]CronHowto

#1

帖子 oneleaf » 2005-07-22 14:14

What is Cron?

Cron is a [daemon] used for scheduling tasks to be executed at a certain time. Each user has a crontab file, allowing them to specify actions and times that they should be executed. There is also a system crontab, allowing tasks such as log rotation and locate database updating to be done regularly.
Using Cron

To use cron, simply add entries to your crontab file. A crontab entry gives a patten specifying when to run and the action to perform, eg:

5 3 * * * /usr/bin/apt-get update

The first part of the crontab entry describes when the action will be performed. There are five fields, separated by whitespace, each of which accept a number, a star (*), or appropriate text. The fields specify, in order, the *minute*, *hour*, *day of month*, *month* and *day of week*. The month and day of week fields allow the use of an abbreviation, such as "jan" for January or "thu" for Thursday.

The example above will execute "/usr/bin/apt-get update" every day of every month at 03:05 (cron assumes 24 hour time). You could have cron annoy you every 5 minutes throughout your work day (9am-5pm) with a message:

*/5 9-17 * * mon,tue,wed,thur,fri wall "Are we there yet?"

or remind you of a birthday at 9 in the morning on the 10th January every year:

0 9 10 jan * echo "It's your mother's birthday today!" > ~/readme

To view the current contents of your crontab file, type:

crontab -l

To edit the file, with the editor specified in your environment (which defaults to vim - :q! is the command to escape without saving if you get stuck and need to read up on it or change your editor), use:

crontab -e

When you exit the editor, the new crontab file will be installed. The file is stored in /var/spool/cron/crontabs but should only be edited via the crontab command.
Further Considerations

The above commands are stored in a crontab file belonging to your user account and executed with your level of permissions. If you want to regularly run a command requiring a greater level of permissions, set up a root crontab file using:

sudo crontab -e

Depending on the commands being run, you may need to expand the root users PATH variable by putting the following line at the top of their crontab file:

PATH=/usr/sbin:/usr/bin:/sbin:/bin

It is sensible to test that your cron jobs work as intended. One method for doing this is to set up the job to run a couple of minutes in the future and then check the results before finalising the timing. You may also find it useful to put the commands into script files that log their success or failure, eg:

echo "Nightly Backup Successful: $(date)" >> /tmp/mybackup.log

For more information, see the man pages for cron and crontab (man is detailed on the BasicCommands page). If your machine is regularly switched off, you may also be interest in at (part of the Ubuntu base install) and anacron (found in the universe repository) that provide other approaches to scheduled tasks.

https://wiki.ubuntu.com/CronHowto
当净其意如虚空,远离妄想及诸取,令心所向皆无碍
头像
gnix_oag
帖子: 1994
注册时间: 2005-05-19 18:38
来自: 湖南永州
联系:

#2

帖子 gnix_oag » 2005-07-23 11:09

What is Cron?

Cron is a [daemon] used for scheduling tasks to be executed at a certain time. Each user has a crontab file, allowing them to specify actions and times that they should be executed. There is also a system crontab, allowing tasks such as log rotation and locate database updating to be done regularly.
什么是Cron

Cron是一个[守护程序]用于在指定的时间内执行行程类的任务,每一个用户都有一个 crontab 文件,来允许他们指定需要执行的内容和时间,此外,系统也有一个 crontab ,用来允许像交替日志和更新本地数据这样有规则的任务。
头像
gnix_oag
帖子: 1994
注册时间: 2005-05-19 18:38
来自: 湖南永州
联系:

#3

帖子 gnix_oag » 2005-07-23 11:35

Using Cron

To use cron, simply add entries to your crontab file. A crontab entry gives a patten specifying when to run and the action to perform, eg:

5 3 * * * /usr/bin/apt-get update
使用 Cron

使用 Cron, 只要在 crontab 文件中添加一些项目,一个 crontab 项目详细说明执行的过程和时间,如:

5 3 * * * /usr/bin/apt-get update
头像
gnix_oag
帖子: 1994
注册时间: 2005-05-19 18:38
来自: 湖南永州
联系:

#4

帖子 gnix_oag » 2005-07-23 11:59

The first part of the crontab entry describes when the action will be performed. There are five fields, separated by whitespace, each of which accept a number, a star (*), or appropriate text. The fields specify, in order, the *minute*, *hour*, *day of month*, *month* and *day of week*. The month and day of week fields allow the use of an abbreviation, such as "jan" for January or "thu" for Thursday.
在crontab 项目里的第一个部分描述了执行任务的时间。它用空格分开成五个区域,每个只能用数字、“*”、或适当的字。这个区域依次说明 “分”,“时”,“每月几日”、“月”、“每周星期几”,月和星期可以用缩写,如 “jan"代替“January" 、"thu" 代替"Thursday"。
头像
gnix_oag
帖子: 1994
注册时间: 2005-05-19 18:38
来自: 湖南永州
联系:

#5

帖子 gnix_oag » 2005-07-23 12:01

The example above will execute "/usr/bin/apt-get update" every day of every month at 03:05 (cron assumes 24 hour time).
上面的例子将在每天的03:05执行“/usr/bin/apt-get update”(cron采用24小时制)
头像
gnix_oag
帖子: 1994
注册时间: 2005-05-19 18:38
来自: 湖南永州
联系:

#6

帖子 gnix_oag » 2005-07-24 10:04

You could have cron you every 5 minutes throughout your work day (9am-5pm) with a message:

*/5 9-17 * * mon,tue,wed,thur,fri wall "Are we there yet?"
你要让cron在所有工作日中每5分钟显示干扰的消息,(上午9点到下午5点)请用

*/5 9-17 * * mon,tue,wed,thur,fri wall "我们在不在?"
上次由 gnix_oag 在 2005-07-24 10:18,总共编辑 2 次。
头像
gnix_oag
帖子: 1994
注册时间: 2005-05-19 18:38
来自: 湖南永州
联系:

#7

帖子 gnix_oag » 2005-07-24 10:11

or remind you of a birthday at 9 in the morning on the 10th January every year:

0 9 10 jan * echo "It's your mother's birthday today!" > ~/readme
或者想使你想起一个生日,在每年正月十日的上午9点,用:

0 9 10 jan * echo "今天是妈妈的生日" > ~/readme
上次由 gnix_oag 在 2005-07-24 10:17,总共编辑 1 次。
头像
gnix_oag
帖子: 1994
注册时间: 2005-05-19 18:38
来自: 湖南永州
联系:

#8

帖子 gnix_oag » 2005-07-24 10:16

To view the current contents of your crontab file, type:

crontab -l
查看 crontab 文件当今的内容,输入:

crontab -l
头像
gnix_oag
帖子: 1994
注册时间: 2005-05-19 18:38
来自: 湖南永州
联系:

#9

帖子 gnix_oag » 2005-07-24 10:24

To edit the file, with the editor specified in your environment (which defaults to vim - :q! is the command to escape without saving if you get stuck and need to read up on it or change your editor), use:

crontab -e
用环境变量指定的编辑器编辑这个文件(),用

crontab -e
头像
gnix_oag
帖子: 1994
注册时间: 2005-05-19 18:38
来自: 湖南永州
联系:

#10

帖子 gnix_oag » 2005-07-24 10:45

When you exit the editor, the new crontab file will be installed. The file is stored in /var/spool/cron/crontabs but should only be edited via the crontab command.
当你关闭编辑器后,新的 crontab 文件将被安装上。用 crontab 命令编辑的文件保存在/var/spool/cron/crontabs
头像
gnix_oag
帖子: 1994
注册时间: 2005-05-19 18:38
来自: 湖南永州
联系:

#11

帖子 gnix_oag » 2005-07-24 10:46

Further Considerations

The above commands are stored in a crontab file belonging to your user account and with your level of permissions. If you want to regularly run a command requiring a greater level of permissions, set up a root crontab file using:

sudo crontab -e
更多的内容

下面的命令保存 crontab 文件需要你的用户有要许可。如果你想要定期运行更大权限的命令, 请设置根 crontab 文件:

sudo crontab -e
头像
gnix_oag
帖子: 1994
注册时间: 2005-05-19 18:38
来自: 湖南永州
联系:

#12

帖子 gnix_oag » 2005-07-24 11:10

Depending on the commands being run, you may need to expand the root users PATH variable by putting the following line at the top of their crontab file:

PATH=/usr/sbin:/usr/bin:/sbin:/bin
依靠命令才能运行的,你可能需要扩大根用户的 PATH 变量,请在他们的 crontab 文件的顶部输入下面的内容:

PATH=/usr/sbin:/usr/bin:/sbin:/bin
头像
gnix_oag
帖子: 1994
注册时间: 2005-05-19 18:38
来自: 湖南永州
联系:

#13

帖子 gnix_oag » 2005-07-24 11:12

It is sensible to test that your cron jobs work as intended. One method for doing this is to set up the job to run a couple of minutes in the future and then check the results before finalising the timing. You may also find it useful to put the commands into script files that log their success or failure, eg:

echo "Nightly Backup Successful: $(date)" >> /tmp/mybackup.log
头像
gnix_oag
帖子: 1994
注册时间: 2005-05-19 18:38
来自: 湖南永州
联系:

#14

帖子 gnix_oag » 2005-07-27 19:09

水平有限,我还不如从书上抄一段呢,书上多的是,
头像
oneleaf
论坛管理员
帖子: 10441
注册时间: 2005-03-27 0:06
系统: Ubuntu 12.04

#15

帖子 oneleaf » 2005-08-22 21:38

谁接下去翻译完?
当净其意如虚空,远离妄想及诸取,令心所向皆无碍
回复