距离最近一次修改已经过去了 413 天了文章的内容可能已经过时,请注意辨别!

什么是计划任务?

后台运行,在预定时间自动执行预设任务。

检查crond服务相关软件包

bash
1
2
3
4
[root@localhost ~]# rpm -qa cron*
crontabs-1.11-6.20121102git.el7.noarch
cronie-anacron-1.4.11-24.el7_9.x86_64
cronie-1.4.11-24.el7_9.x86_64

出现如上信息说明已安装!

检查crond服务是否运行

bash
1
systemctl status crond
bash
1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost ~]# systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since 三 2022-07-27 08:20:21 CST; 4min 9s ago
Main PID: 705 (crond)
CGroup: /system.slice/crond.service
└─705 /usr/sbin/crond -n

7月 27 08:20:21 localhost.localdomain systemd[1]: Started Command Scheduler.
7月 27 08:20:21 localhost.localdomain crond[705]: (CRON) INFO (RANDOM_DELAY ...
7月 27 08:20:21 localhost.localdomain crond[705]: (CRON) INFO (running with ...
Hint: Some lines were ellipsized, use -l to show in full.

running说明正在运行

crond定时任务服务应用

Cron是Linux系统中以后台进程模式周期性执行命令或指定程序任务的服务软件名。

Linux系统启动后,cron软件便会启动,对应进程名为crond。

默认定期(每分钟)检查系统是否有需要执行的任务计划,如果有,则按计划执行。

cron定时任务的名字

crond定时任务进程名

crontab管理定会任务命令

为什么要使用定时任务?

  • 数据库定时备份
  • 夜间网站数据(用户上传、文件、图片、程序)备份
  • 备份时间等待长
  • 任务重复性高

利用Linux的定时任务cron工具可以解决重复性、周期性、自动备份等运维工作。

Linux下定时任务软件

  1. at轻量定时任务工具,依赖于atd服务,临时
  2. crond定时任务工具

at

语法

bash
1
at [Time] [Commend]

Ctrl+D提交任务

时间

bash
1
2
3
4
5
6
7
8
HH:MM
YYYY-mm-dd
noon 正午十二点
midnight 夜间十二点
teatime 下午茶时间、四点
tomorrow 明天
now+1min 一分钟后
now+1minutes/hours/days/weeks

cron

crond进程提交任务方式与at不同,crond需要读取配置文件,且有固定的文件格式,通过crontab命令管理文件

cron任务分为两类

系统定时任务

操作系统自带的。crond服务除了会在工作时查看/var/spool/cron文件夹下的定时任务文件以外,还会看/etc/cron.d目录以及/etc/anacrontab下面的文件内容,里面存放着每天每周每月需要执行的系统任务

bash
1
2
3
4
5
6
7
8
9
[root@localhost ~]# ls /etc/ -l|grep cron*
-rw-------. 1 root root 541 1月 14 2022 anacrontab
drwxr-xr-x. 2 root root 21 7月 13 12:25 cron.d
drwxr-xr-x. 2 root root 42 7月 13 12:25 cron.daily
-rw-------. 1 root root 0 1月 14 2022 cron.deny
drwxr-xr-x. 2 root root 22 7月 13 12:25 cron.hourly
drwxr-xr-x. 2 root root 6 6月 10 2014 cron.monthly
-rw-r--r--. 1 root root 451 6月 10 2014 crontab
drwxr-xr-x. 2 root root 6 6月 10 2014 cron.weekly

用户定时任务

当系统管理员或普通用户创建了需要定期执行的任务,可以使用crontab命令配置,当crond服务在启动时,每分钟查看/var/spool/cron路径下以系统用户名命名的定时任务文件,以确定是否有需要执行的任务。

crontab命令

参数

bash
1
2
3
4
5
-l 查看定时任务
-e 编辑定时任务,建议手动编辑
-i 删除定时任务,提示用户确认删除,避免出错
-r 删除定时任务,移除/var/spool/cron/<username>文件
-u <user> 指定用户执行任务,root可以管理普通用户计划任务

语法

* * * * * <命令绝对路径>

口诀:什么时间做什么事

编写时注意书写规范

  • 加上注释:what who why when
  • 不要求输出定向到 >/dev/null 2>&1
  • 在指定用户下执行相关定时任务,而不是什么都交给root,可以使用-u
  • 推荐使用crontab -e编辑,有语法检查
bash
1
2
* * * * * 开头五颗星用于设置时间
第二部分是执行的命令

五颗星分别对应:

  • 分(0-59)
  • 时(0-23)
  • 日(1-31)
  • 月(1-12 or jan、feb…..)
  • 周(0-6、sunday=0/7 or sun、mon……)

注意:每天和每周不能同时使用

特殊符号

bash
1
2
3
4
*	每
- 范围,1-12表示1到12
, 分割时段,17,20表示17和20
/n n表示可以整除的数字,每隔n的单位时间,如每隔10分钟表示/10

管理命令

查看cron运行状态

bash
1
systemctl is-active crond
bash
1
systemctl status crond

定时任务与邮件服务

计划任务触发执行后,会通过邮件发送给用户(并非互联网中的邮件那样,仅仅是系统内部的邮件服务)

检查服务端口

邮件端口号为25

bash
1
2
ss -tnl | grep 25
netstat -tnl | grep 25

若未开启服务

需要启动postfix服务,用于发送邮件

首先,编辑文件

bash
1
vim /etc/postfix/main.cf

然后,修改参数

bash
1
2
3
4
...
inet_interface = all
inet_protocols = all
...

最后,启动postfix服务

bash
1
systemctl start postfix

本地化电子邮件服务

邮件协议解释

  • pop3
  • smtp
  • imap

mailx命令

三个概念

MTA:Mail Transport Agent,邮件传送代理,也就是postfix服务

MUA:Mail User Agent,收发邮件的客户端

Centos7通过mailx发送邮件,通过mail收邮件(mail也可以用来发邮件)

bash
1
2
3
4
5
6
mailx -s "This is title." usr
#-s 表示添加主题
#usr 为发送邮件的对象
#回车后,输入文章内容
#输入.号,退出邮件
#EOT 为结束符号end out

使用文件中的内容

bash
1
mail -s "This is title." usr <mailtosb.txt

可以将文件中的内容当作邮件发给对应用户

mail命令

用于接收邮件

查看邮件

如果有多封邮件,&+第一列对应的数字即可打开对应邮件。

q退出

cron定时任务实践