已索引

crond 用来做什么

crond 用来定义在什么时间做什么事儿,这个事儿就是命令或者脚本,时间的写法则较为灵活。
Cron checks these files and directories (man 8 crond):

 /etc/crontab (/etc/anacrontab)
        system crontab.  Nowadays the file is empty by default. Originally it was usually used to run daily, weekly, monthly
        jobs.  By default these jobs are now run through anacron which reads /etc/anacrontab configuration file. See
        anacrontab(5) for more details.

 /etc/cron.d/
        directory that contains system cron jobs stored for different users.

 /var/spool/cron
              directory that contains user crontables created by the crontab command.

其中,我们用到最多的就是 /var/spool/cron 这个位置,即 crontab 命令。

/var/spool/cron (crontab)

在管理员未做限制的情况下,每个用户都可以使用 crontab 命令来查看(-l)和编辑(-e)自己的计划任务。
root用户还可以去编辑或查看其他用户的计划任务(-u)。
不建议 root 用户使用 -u 选项去为其它用户生成计划任务,虽然目前没有发现问题,但不建议,原因如下所示:

目前普通用户没有计划任务:

# ll /var/spool/cron/
total 4
-rw------- 1 root root 109 Dec 19 16:43 root

使用 root 用户为普通用户创建一个计划任务:

# crontab -e -u xuwang
no crontab for xuwang - using an empty one
crontab: installing new crontab
#
# ll /var/spool/cron/
total 8
-rw------- 1 root root 109 Dec 19 16:43 root
-rw------- 1 root root  21 Dec 19 17:38 xuwang
#

可以看到,这个普通用户计划任务对应的文件属主是 root.root(虽并不影响其计划任务的运行,但会为管理带来困惑)。
但切换到 xuwang 用户通过 crontab -e 命令修改计划任务后,文件的属主变回 xuwang.xuwang。

文件格式

文件的内容格式为(注意在计划任务中不需要写用户身份):
分 时 日 月 周 命令

例如:
0 22 1-5 mail -s "It's 10pm" joe%Joe,%%Where are your kids?%

注意:
1、注释符号 # 只能出现在行首,不能出现在命令区域,因为这样会把 # 作为命令的一部分。
2、命令中的 % 是换行符

时间格式

接下来我们说一下时间的写法(man 5 crontab):

       The time and date fields are:

              field          allowed values
              -----          --------------
              minute         0-59
              hour           0-23
              day of month   1-31
              month          1-12 (or names, see below)
              day of week    0-7 (0 or 7 is Sunday, or use names)

* 表示 "first-last".
8-11 表示 8,9,10,11
0-4,8-12 表示 0,1,2,3,4,8,9,10,11,12
0-23/2 表示 every other hour, 即 "0,2,4,6,8,10,12,14,16,18,20,22". [这儿以hour列为例]
*/2 表示 run every two hours
1-59/2 每2分钟(奇数钟),即1,3,5,7,9…… [这儿以minute列为例]
0-58/2 每2分钟(偶数钟),即0,2,4,6,8…… [这儿以minute列为例]
0-59/5 每5分钟,即0,5,10,15…… [这儿以minute列为例]

/n,n 表示 Step values,即步长,而不是表示除。

如果在计划任务中同时指定了天和周,则都执行:
Note: The day of a command's execution can be specified by two fields: day of month, and day of week.
If both fields are restricted (ie, aren't *), the command will be run when either field matches the current time.
For example, "30 4 1,15 * 5" would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday.

EXTENSIONS

   These special time specification "nicknames" which replace the 5 initial time and date fields, and are prefixed with the '@' character, are supported:

   @reboot    :    Run once after reboot.
   @yearly    :    Run once a year, ie.  "0 0 1 1 *".
   @annually  :    Run once a year, ie.  "0 0 1 1 *".
   @monthly   :    Run once a month, ie. "0 0 1 * *".
   @weekly    :    Run once a week, ie.  "0 0 * * 0".
   @daily     :    Run once a day, ie.   "0 0 * * *".
   @hourly    :    Run once an hour, ie. "0 * * * *".

/var/spool/cron

Cron searches /var/spool/cron for crontab files which are named after accounts in /etc/passwd; The found crontabs are loaded into the memory.

正常情况下,该目录下的文件只有一个名为 root 的文件,当某个用户创建了自己的计划任务后,就会在该目录下生成同名的文件,如:

# ll /var/spool/cron/
total 8
-rw------- 1 root   root   109 Dec 19 16:43 root
-rw------- 1 xuwang xuwang  20 Dec 19 17:41 xuwang

这是正常情况下的样子,如果不是这样子,则可能发生异常。案例如下:

/etc/crontab

system crontab. Nowadays the file is empty by default. Originally it was usually used to run daily, weekly, monthly

  1. By default these jobs are now run through anacron which reads /etc/anacrontab configuration file. See
    anacrontab(5) for more details.

文件给出了写法格式,按照格式书写即可。
注意:在 /etc/crontab 中多出一列,指明计划任务的用户身份。

/etc/anacrontab

Anacron is used to execute commands periodically, with a frequency specified in days. Unlike cron(8), it does not assume that the machine is running continuously. Hence, it can be used on machines that are not running 24 hours a day to control regular jobs as daily, weekly, and monthly jobs.

Anacron reads a list of jobs from the /etc/anacrontab configuration file (see anacrontab(5)). This file contains the list of jobs that Anacron controls. Each job entry specifies a period in days, a delay in minutes, a unique job identifier, and a shell command.

For each job, Anacron checks whether this job has been executed in the last n days, where n is the time period specified for that job. If a job has not been executed in n days or more, Anacron runs the job's shell command, after waiting for the number of minutes specified as the delay parameter.

After the command exits, Anacron records the date (excludes the hour) in a special timestamp file for that job, so it knows when to execute that job again.

When there are no more jobs to be run, Anacron exits.

/var/spool/anacron: This directory is used by Anacron for storing timestamp files.

# cat /var/spool/anacron/cron.daily
20231229

系统默认的周期性任务:

# cat /etc/anacrontab
# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22

#period in days   delay in minutes   job-identifier   command
1       5       cron.daily              nice run-parts /etc/cron.daily
7       25      cron.weekly             nice run-parts /etc/cron.weekly
@monthly 45     cron.monthly            nice run-parts /etc/cron.monthly

每天的任务:

# ll /etc/cron.daily/
total 12
-rwx------. 1 root root 219 Oct 31  2018 logrotate
-rwxr-xr-x. 1 root root 618 Oct 30  2018 man-db.cron
-rwx------. 1 root root 208 Apr 11  2018 mlocate

每周的任务:

# ll /etc/cron.weekly
total 0

每月的任务:

# ll  /etc/cron.monthly
total 0
-- By 许望(RHCA、OCM、VCP)
最后修改:2023 年 12 月 29 日 04 : 22 PM
如果觉得我的文章对你有用,请随意赞赏