Linux下使用Cron定义系统作业

cron 守护进程可以配置为运行预定的系统作业。
对于系统作业,还必须指定具有运行命令权限的用户。
在时间定义(前五个字段)和命令(现在成为第七个字段)之间输入用户名。

我们可以在 /etc/crontab 文件或者 /etc/cron.d/ 目录中的文件中定义系统作业。
CentOS/RHEL 系统默认的 crontab 如下图所示。

# 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

注意:虽然我们可以向 /etc/crontab 添加其他行,但不应删除默认行。

上述 /etc/crontab 文件中定义的作业按指示的时间间隔运行包含在以下目录中的脚本:

目录间隔
/etc/cron.hourly每小时运行的作业。
/etc/cron.daily每天运行的作业。
/etc/cron.weekly每周运行的作业。
/etc/cron.monthly每月运行的作业。
# ls -lrt /etc/cron.hourly/
total 4
-rwxr-xr-x. 1 root root 392 Mar 29  2017 0anacron
[root@jamiericho4 ~]#  ls -l /etc/cron*
-rw-------. 1 root root   0 Mar 29  2017 /etc/cron.deny
-rw-r--r--. 1 root root 451 Dec 27  2013 /etc/crontab
/etc/cron.d:
total 16
-rw-r--r--. 1 root root 128 Mar 29  2017 0hourly
-rw-r--r--. 1 root root 173 Sep 23  2016 awslogs
-rw-r--r--. 1 root root 179 Sep 23  2016 awslogs_log_rotate
-rw-r--r--. 1 root root 108 Jun 13  2017 raid-check
/etc/cron.daily:
total 16
-rwx------. 1 root root 219 Jan 24  2017 logrotate
-rwxr-xr-x. 1 root root 618 Mar 17  2014 man-db.cron
-rwx------. 1 root root 208 Nov  4  2016 mlocate
-rwx------. 1 root root 256 Sep  1 10:34 rhsmd
/etc/cron.hourly:
total 4
-rwxr-xr-x. 1 root root 392 Mar 29  2017 0anacron
/etc/cron.monthly:
total 0
/etc/cron.weekly:
total 0
日期:2020-09-17 00:14:23 来源:oir作者:oir