Linux中Crontab的使用详解(翻译)

Cron来源于希腊单词chronos(意为时间),是Linux系统下一个自动执行指定任务的程序。例如,你想在每晚睡觉期间创建某些文件或文件夹的备份,就可以用Cron来自动执行。(Cron comes from the word chronos, the Greek word for time. Cron is a utility that
can help with automating certain tasks in Linux. For example if you would like
to create backups of certain files or directories each night while you are
sleeping, you can use Cron to automate this.
)

Cron
stores it’s enteries in the crontab (cron table) file. This is generally
located in your /etc directory. As well, each user on your system can have
their own crontab which would be stored in /var/spool/cron/. To edit a users
crontab entry, simply log into your system for that particular user and type
crontab -e. The default editor for the ‘crontab -e’ command is vi. If you are
not familiar with VI you can change the default editor by running the following
command export VISUAL=’editor’. Of course you must replace editor with your
favorite text editor (nano, pico, joe etc). Or you could always learn how to
use VI; )

cron把命令行保存在crontabcron table)文件里,这个文件通常在 /etc 目录下。每个系统用户都可以有自己的crontab(在 /var/spool/cron/ 下)。要查看当前用户的crontab,输入 crontab -l;要编辑crontab,输入 crontab -e;要删除crontab,输入 crontab -r。如当前是root身份,要查看/编辑/删除/某用户的crontab,只需在相应的命令后加上 -u USERNAME(如 crontab -e -u USERNAME)即可。crontab文件的默认编辑器是vi,可以输入 export VISUAL=’editor’ 更改默认编辑器。

Below
are 2 entries in my /etc/crontab file. The first one is used to back up my /etc
directory nightly. The second entry is to run the Analog program to calculate
the web server stats for linuxhelp.ca.

以下是两个cron语句的例子(在 /etc/crontab 文件里)。前者用来晚间备份 /etc 目录,后者运行Analog程序处理服务器的统计信息。

12 3 * * * root tar czf /usr/local/backups/daily/etc.tar.gz /etc >> /dev/null
2>&1

52 5 * * * root /usr/local/src/analog-5.32-lh/analog >> /dev/null 2>&1

Below
is a table of what each field does.

以下是cron语句中的字段与字段说明:

Field  Meaning

1   Minute (0-59)

2   Hour (2-24)

3   Day of month (1-31)

4   Month (1-12, Jan, Feb, etc)

5   Day of week (0-6) 0 = Sunday, 1 = Monday etc
or Sun, Mon, etc)

6   User that the command will run as

7   Command to execute

字段   说明

1   分钟(0-59

2   小时(2-24

3   日期(1-31

4   月份(1-12;或英文缩写JanFeb等)

5   周几(0-60为周日;或单词缩写SunMon等)

6   用户名(执行命令时以此用户的身份)

7   要执行的命令(路径)

So using one of our original examples:

现在分析我们上面的一个例子:

12 3 * * * root tar czf /usr/local/backups/daily/etc.tar.gz /etc >> /dev/null
2>&1

This
will run tar czvf /usr/local/backups/daily/etc.tar.gz /etcat 3:12am every day.
The >> /dev/null 2>&1 part means to send any standard output to
/dev/null (the linux trash can) and to redirect standard error (2) to the same
place as the standard output (1). Basically it runs the command without any
output to a terminal etc.

这条语句将在每天的凌晨312分(03:12)运行 tar czf /usr/local/backups/daily/etc.tar.gz /etc 命令。>> /dev/null 2>&1 表示把所有标准输出发送到 /dev/nulllinux的回收站),把标准错误输出(2)发送到和标准输出(1)同样的地方(即 /dev/null)。运行这行命令将不会产生任何输出。

Another
example of a more complex entry would be:

这条语句可以变得稍微复杂一点:

30 15 13 6 1 * root tar czf /usr/local/backups/daily/etc.tar.gz /etc >>
/dev/null 2>&1

This
will run tar czvf /usr/local/backups/daily/etc.tar.gz /etcon Monday June 13th
at 3:30pm.

它将在613日周一的15:30运行 tar czf
/usr/local/backups/daily/etc.tar.gz /etc
命令。

You could
also use the following to achieve the same results

以下语句可以达到同样的效果:

30 15 13 Jun Mon * root tar czf /usr/local/backups/daily/etc.tar.gz /etc >>
/dev/null 2>&1

If you
wanted to run a command as user joey 15 minutes after every hour regardless of
the date you could add the following entry:

如果你想以用户joey的身份每小时的第15分钟运行某个程序,可以使用:

15 * * * * joey /usr/bin/somecommand >> /dev/null 2>&1

The
astrix ‘*’ in the example above is a wildcard meaning that cron will ignore the
field.

其中的星号(*)是通配符,表示cron将忽略这个字段。

If you
wanted to run a command every 2 hours you could enter in */2 for the hour
field. This would run the specified command at 2am, 4am, 6am, 8am, 10am, 12pm,
2pm, and so on. An example of this type of entry would be:

如果你想每两小时就运行某个程序,可以在小时字段里使用 */2。它将会在2点,4点,6……22点,24点运行。具体语句如下:

0 */2 * * * joey /usr/bin/somecommand >> /dev/null 2>&1

You
can also use commas to specify more than one time per entry. For instance if
you wanted to run a command at 15 and 30 past each hour you would enter in
15,30 for the minute field. An example of this type of entry would be:

cron语句中还可以使用逗号(,)来指定多个时间。例如你想在每小时的15分和30分运行某个程序,可以在分钟字段使用15,30

15,30 * * * * joey /usr/bin/somecommand >> /dev/null 2>&1

If you
wanted to run a command every day at a certain time for the first week of the
month you would enter in 1-7 for the day field. An example of this type of
entry would be:

如果你想在每月的第一周(即1号到7号)每天的指定时间运行某个程序,可以在日期字段使用 1-7

15,30 */2 1-7 * * joey /usr/bin/somecommand >> /dev/null 2>&1

This
would run somecommand every 2 hours at the 15’s and 30’s (2:15, 2:30, 4:15,
4:30 etc) for the first 7 days of the month.

这条语句将在每月的第1-7日每两小时的15分和30分(02:1502:30……22: 1522:30等)运行 /usr/bin/somecommand 命令。

If you
want cron to execute a bunch of scripts at 4:18pm every day you could put all
of the scripts in one directory (for example, /home/username/cron) and add the
following line to your crontab:

如果你想在每天的16:18执行一个脚本集合,可以把所有要执行的脚本放到一个目录中(如 /home/username/cron),可以使用:

18 16 * * * root run-parts /home/username/cron >> /dev/null 2>&1

If you
wanted to save the output of a certain command you can replace the >>
/dev/null 2>&1 with >> /home/user/somecommand.log 2>&1

如果你想保存某个程序的输出结果,
可以把
>> /dev/null 2>&1 替换为>>
/home/user/somecommand.log 2>&1

After
you’ve added all your entries you can use the command crontab -l to list them.

查看当前用户的cron配置,使用 crontab -l

If you
wanted to remove your crontab file you could run crontab -r to delete it.

删除当前用户的cron配置,使用 crontab -r

To
edit a users crontab file as root you can run crontab -e -u username

编辑当前用户的cron配置,使用 crontab –e,以root身份查看/编辑/删除某用户的cron配置,在命令后加上 -u USERNAME

So as you can see, Cron is very configurable and is a
great tool for every system administrator to automate tasks.

所以你可以发现,Cron很容易配置,是让每个系统管理员实现任务自动化的一个非常好的工具。

 

翻译原文:

http://www.linuxhelp.net/guides/cron/

 

Tonitech版权所有 | 转载请注明出处: http://www.tonitech.com/473.html

发表评论