Product SiteDocumentation Site

9.7. 使用 cronatd运行计划任务

cron is the daemon responsible for executing scheduled and recurring commands (every hour, every day, every week, etc.). atd deals with commands to be executed a single time, but at a specific moment in the future.
在Unix 系统,许多任务都被周期性的调度:
默认情况下,所有用户都可以运行计划任务。每个用户有他们自己的 crontab,其中记录了要调度的命令。可以运行 crontab -e 编辑(其内容存储在 /var/spool/cron/crontabs/user 文件中)。
root用户有自己的 crontab,但是也可以使用 /etc/crontab 文件,或者在 /etc/cron.d 目录中写入另外的crontab 文件。这两种方法可以用来指明执行命令时的用户身份。
cron 软件包默认包含一些可执行的调度命令:
很多 Debian 软件包依赖此服务:把维护脚本置于此文件夹,确保其服务的最佳运作。

9.7.1. crontab 文件的格式

Each significant line of a crontab entry describes a scheduled command with the six (or seven) following fields:
  • the value for the minute (from 0 to 59);
  • 小时值(从0到23);
  • 每月的日期值(从1到31);
  • 月份值(从1到12);
  • 星期值(从0到7,1对应星期一,0和7都代表星期日;也可以使用星期英文表示单次的前三个字母,例如 SunMon等);
  • 命令将以哪个用户的名义执行(在文件 /etc/crontab/etc/cron.d/中,而不是在用户自己的调度文件中);
  • 要执行的命令(当满足前面5栏定义的条件时)。
全部的细节记录在 crontab(5) 手册中。
Each value can also be expressed in the form of a list of possible values (separated by commas). The syntax a-b describes the interval of all the values between a and b. The syntax a-b/c describes the interval with an increment of c (example: 0-10/2 means 0,2,4,6,8,10). An asterisk * is a wildcard, representing all possible values.

例 9.2. Sample user crontab file

#Format
#min hour day mon dow  command

# Download data every night at 7:25 pm
 25  19   *   *   *    $HOME/bin/get.pl

# 8:00 am, on weekdays (Monday through Friday)
 00  08   *   *   1-5  $HOME/bin/dosomething

# every two hours
 *  */2   *   *   *    $HOME/bin/dosomethingelse

# Restart the IRC proxy after each reboot
@reboot /usr/bin/dircproxy

9.7.2. 使用 at 命令

The at executes a command at a specified moment in the future. It takes the desired time and date as command-line parameters, and the command to be executed in its standard input. The command will be executed as if it had been entered in the current shell. at even takes care to retain the current environment, in order to reproduce the same conditions when it executes the command. The time is indicated by following the usual conventions: 16:12 or 4:12pm represents 4:12 pm. The date can be specified in several European and Western formats, including DD.MM.YY (27.07.22 thus representing 27 July 2022), YYYY-MM-DD (this same date being expressed as 2022-07-27), MM/DD/[CC]YY (i.e., 12/25/22 or 12/25/2022 will be December 25, 2022), or simple MMDD[CC]YY (so that 122522 or 12252022 will, likewise, represent December 25, 2022). Without it, the command will be executed as soon as the clock reaches the time indicated (the same day, or tomorrow if that time has already passed on the same day). You can also simply write “today” or “tomorrow”, which is self-explanatory.
$ at 09:00 27.07.22 <<END
> echo "Don't forget to wish a Happy Birthday to Raphaël!" \
>   | mail [email protected]
> END
warning: commands will be executed using /bin/sh
job 1 at Wed Jul 27 09:00:00 2022
另外一种方法可以向后推迟一段时间执行命令:at now + number periodperiod 可以是 minutes, hours, days, 或者 weeksnumber 指明在命令执行之前消逝时间的单位数量。
要取消 cron中的计划任务,运行 crontab -e 删除 crontab 文件中的对应行。对于使用 at 命令的任务,同样简单:运行 atrm 任务编号。任务编号在调度时由 at 命令指定,可以通过 atq 命令查询找到,该命令给出当前计划任务清单。