CentOS

查看配置

[root@ecs ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

操作cron

  • 创建定时任务

    [root@ecs ~]# crontab -e
    0 0 */1 * * /data/backup/backup.sh

    :wq保存退出。

    每天00:00执行备份任务

    [root@ecs ~]# echo "0 0 */1 * * root /data/backup/backup.sh" >> /etc/crontab

    或者:

    [root@ecs ~]# cat >> /etc/cron.d/backup << EOF
    SHELL=/bin/bash
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    0 0 */1 * * root /data/backup/backup.sh
    EOF
    [root@ecs ~]# chmod 600 /etc/cron.d/backup

    测试任务

    [root@ecs ~]# cat > /usr/local/src/test.sh << EOF
    #!/bin/sh
     
    date '+%Y-%m-%d %H:%M:%S' >> /usr/local/src/test.log
    EOF
    [root@ecs ~]# chmod a+x /usr/local/src/test.sh
    [root@ecs ~]# cat > /etc/cron.d/test << EOF
    SHELL=/bin/bash
    */1 * * * * root /usr/local/src/test.sh
    EOF
    [root@ecs ~]# chmod 600 /etc/cron.d/test

    注:

    • cron服务出于安全考虑,要求/etc/cron.d/目录下的文件不能拥有全局写权限,即仅允许执行用户可写(600),否则会在/var/log/cron日志中报错:crond[8162]: (root) BAD FILE MODE (/etc/cron.d/test)
    • /etc/cron.d/下的文件仅是配置文件,因此无需可执行权限。
  • 重启crond服务

    [root@ecs ~]# systemctl restart crond

    修改定时任务后,必须重启crond服务才会生效。

Ubuntu

安装cron

root@ecs:~# apt-get install cron

操作cron

  • 启动服务

    root@ecs:~# /etc/init.d/cron start

    或者:

    root@ecs:~# systemctl enable --now cron
  • 查看是否启动

    root@ecs:~# pgrep cron

    或者:

    root@ecs:~# ps aux | grep cron
  • 关闭服务

    root@ecs:~# /etc/init.d/cron stop
  • 重启服务

    root@ecs:~# /etc/init.d/cron restart
  • 重新加载配置

    root@ecs:~# /etc/init.d/cron reload
  • 添加定时任务

    root@ecs:~# crontab -e
    0 0 */1 * * /data/backup/backup.sh
    root@ecs:~# chmod a+x /data/backup/backup.sh

    crontab -e如果报错:Couldn't find an editor!则需要先安装编辑器:apt-get install nano vim(nano和vim是两个独立的编辑器,选其一即可)。

查看配置

root@ecs:~# cat /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
# You can also override PATH, but by default, newer versions inherit it from the environment
#PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#

时间格式

符号说明
*代表取值范围内的所有数字
/代表每隔(/5,从最小值(0)开始,每隔5个单位)
-代表范围(1-5,表示1到5之间所有值,即1、2、3、4和5)
,代表多个(1,3,5,表示1、3和5)

run-parts

无论是CentOS还是Ubuntu,都可以通过run-parts命令,按顺序运行/etc/cron.hourly/etc/cron.daily/etc/cron.weekly/etc/cron.mouthly四个目录内的所有可执行脚本。

注:

  • run-parts会忽略文件名中包含点号的文件,如脚本文件名为backup.sh则被忽略。
  • 必须赋予脚本可执行权限,否则定时任务无法执行。

CentOS运行机制

  1. crond会自动运行/etc/cron.d/0hourly
  2. /etc/cron.d/0hourly01 * * * * root run-parts /etc/cron.hourly规定每小时的第1分钟执行run-parts /etc/cron.hourly
  3. /etc/cron.hourly/0anacron中执行anacron,anacron会自动调用run-parts执行/etc/cron.daily/etc/cron.weekly/etc/cron.mouthly目录内的所有脚本。

Ubuntu运行机制

执行规则直接定义在/etc/crontab

  • 17 * * * *执行run-parts --report /etc/cron.hourly
  • 25 6 * * *执行run-parts --report /etc/cron.daily
  • 47 6 * * 7执行run-parts --report /etc/cron.weekly
  • 52 6 1 * *执行run-parts --report /etc/cron.monthly

操作run-parts

  • 列出被run-parts识别的脚本

    root@ecs:~# run-parts --test /etc/cron.hourly
  • 手动运行所有hourly脚本

    root@ecs:~# run-parts --verbose /etc/cron.hourly

如果定时任务规定了严格的执行时间,优先建议使用crontab -e/etc/crontab定义任务执行时间,或者在/etc/cron.d/目录中自定义脚本,再在脚本中定义任务执行时间。

如果定时任务只需要每天、每周或每月执行一次,则优先推荐/etc/cron.daily/etc/cron.weekly/etc/cron.monthly目录内创建任务脚本,因为这3个目录内的定时任务使用anacron进行管理。anacron执行定时任务并没有严格的执行时间,而是根据时间间隔来决定下次执行的时间,这对于关机状态下的定时任务非常友好。

例如:一个每周执行一次的计划任务,第一次执行的时间为周一早上09:00,如果使用cron,那么下次执行的时间则是第二周的周一早上09:00,如果第二周的周一早上服务器是处于关闭状态的,那么这个计划任务就无法执行,必须等到第三周的周一早上09:00才会再次执行。

而使用anacron,即使第二周的周一09:00服务器处于关闭状态,接下来假设周二开启了服务器,anacron会检测上次执行的时间是否超过一个星期,一旦超过一个星期就会再次执行。

/etc/cron.daily/etc/cron.weekly/etc/cron.monthly由anacron接管,/etc/cron.daily依旧由cron管理。

标签: none

添加新评论