Anolis8

AnolisOS 8 Crontab定时任务

2022-10-04  本文已影响0人  前浪浪奔浪流

一、crontab介绍

Crontab是系统定时任务计划,也称之为系统作业,用于周期性的执行程序。crontab定时任务的工作主要分为以下两类。

系统任务:系统周期性所要执行的工作,如备份系统数据、清理缓存等;
用户任务:某个用户定期要做的工作,例如每隔几分钟检查邮件服务器是否有新信,这些工作可由每个用户自行设置等。

二、crontab常用命令

2.1、查看Crontab作业状态

crond 服务一般在AnolisOS 8系统中默认安装,用如下命令查看Crontab定时任务。

systemctl status crond
[root@anolis8 ~]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2022-10-01 17:50:25 CST; 2 days ago
 Main PID: 1066 (crond)
    Tasks: 1 (limit: 23664)
   Memory: 1.6M
   CGroup: /system.slice/crond.service
           └─1066 /usr/sbin/crond -n

10月 04 06:01:01 anolis8 run-parts[3787]: (/etc/cron.hourly) starting 0anacron
10月 04 07:01:01 anolis8 CROND[3803]: (root) CMD (run-parts /etc/cron.hourly)
10月 04 07:01:01 anolis8 run-parts[3812]: (/etc/cron.hourly) finished 0anacron
10月 04 08:01:01 anolis8 CROND[3832]: (root) CMD (run-parts /etc/cron.hourly)
10月 04 08:01:01 anolis8 run-parts[3841]: (/etc/cron.hourly) finished 0anacron
10月 04 09:01:01 anolis8 CROND[3856]: (root) CMD (run-parts /etc/cron.hourly)
10月 04 09:01:01 anolis8 run-parts[3865]: (/etc/cron.hourly) finished 0anacron
10月 04 10:01:01 anolis8 CROND[3876]: (root) CMD (run-parts /etc/cron.hourly)
10月 04 10:01:01 anolis8 run-parts[3885]: (/etc/cron.hourly) finished 0anacron
10月 04 11:01:01 anolis8 CROND[4020]: (root) CMD (run-parts /etc/cron.hourly)

如果不存在,我们可以用这个命令安装。

dnf install crontabs
[root@anolis8 ~]# dnf install crontabs
Repository epel is listed more than once in the configuration
上次元数据过期检查:0:18:38 前,执行于 2022年10月04日 星期二 11时03分55秒。
软件包 crontabs-1.11-17.20190603git.an8.noarch 已安装。
依赖关系解决。
无需任何处理。
完毕!

2.2、查看指定用户的Crontab作业

例如查看AnolisOS 8系统中用户nobody的Crontab定时任务,不加-u参数,则查看当前用户的作业。

crontab -l -u nobody
[root@anolis8 ~]# crontab -l
no crontab for root

2.3、编辑指定用户的Crontab作业

如果目录已经存在指定的文件(文件名为当前用户名),则会编辑文件,否则将会以当前用户名创建作业文件,注意/var/spool/cron是用户任务,/etc/crontab是系统任务。

[root@anolis8 ~]# ll /var/spool/cron
总用量 0
[root@anolis8 ~]# 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
crontab -e -u nobody   #编辑指定用户的定时任务
crontab -e    #编辑当前用户的定时任务

2.4、删除指定用户的所有Crontab作业

crontab -r -u nobody

2.5、重启Crontab作业

systemctl restart crond

2.6、停止Crontab作业

systemctl stop crond

2.7、查看Crontab版本

[root@anolis8 ~]# crond -V
cronie 1.5.2

三、Crontab文件

3.1、Crontab日志位置

在 /var/log目录下,cron打头的就是系统作业执行的日志文件。

[root@anolis8 ~]# ll /var/log/cron*
-rw-------  1 root root 15253 10月  4 11:23 /var/log/cron
-rw-------. 1 root root 26263 10月  2 03:13 /var/log/cron-20221002

3.2、Crontab位置

在 /etc/crontab目录下,这些任务都会是系统在运行起来后自动进行调度的。

[root@anolis8 ~]# 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

3.3、禁止使用Crontab命令的用户

在 /etc/cron.deny 表示不能使用Crontab命令的用户。

3.4、允许使用Crontab命令的用户

在 /etc/cron.allow 表示能使用Crontab的用户。如果/etc/cron.deny和/etc/cron.allow同时存在,那么/etc/cron.allow 优先。如果两个文件都不存在,那么只有root用户可以安排作业。

[root@anolis8 ~]# cat /etc/cron.deny
[root@anolis8 ~]# cat /etc/cron.allow
cat: /etc/cron.allow: 没有那个文件或目录

四、Crontab作业的使用

4.1、Crontab创建

crontab -e
此时会进入到vi编辑界面,来编写我们要调度的Crontab定时任务
* * * * * command // 前五个字段可以取整数值,指定何时开始工作,第六个域是命令字段。 字段之间用空格分割。
每小时的第几分钟执行,取值 0-59
每日的第几个小时执行,取值 0-23
每月的第几天执行,取值 1-31
每年的第几月执行,取值 1-12
每周的第几天执行,取值 0-6

4.2、Crontab命令中的一些常用特殊符号

表示任何时刻
,表示分割
-表示一个段,如第二段里: 1-5,就表示1到5点
/n表示每个n的单位执行一次,
/1, 就表示每隔1个小时执行一次命令。也可以写成1-23/1

4.3、Crontab常用示例

例如:每天8点、12点、16点的0分钟执行以下tmp目录下的sh文件。

00 8,12,16 * * * /tmp/test.sh

例如:每天的21:43 执行

43 21 * * * /tmp/test.sh

例如: 每周一的 17:00 执行

0 17 * * 1 /tmp/test.sh

例如:周一到周六 21:00 执行

0 21 * * 1-6 /tmp/test.sh

上一篇 下一篇

猜你喜欢

热点阅读