第16章 Linux计划任务与日志的管理
- at
- crontab
- 日志记录
第17章 Linux系统启动原理及故障排除
第18章 Linux网络管理技术
- 网络相关命令
Linux运维实战:CentOS7.6操作系统从入门到精通(16-18)
第16章 Linux计划任务与日志的管理
16.1 at计划任务及crontab定时任务的使用方法
任务调度分两种:系统任务调度、用户任务调度。
计划任务的安排方式分两种:一种是突发性的,就是这个任务是临时决定的,只执行一次,这种计划任务在Linux中被称为at计划任务。另一种是定时性的,也就是每隔一定的周期就要重复执行,也称为周期性的计划任务,即crontab计划任务。
16.1.1 at计划任务的使用
命令使用格式:at 时间
。
使用at命令,需要开启atd服务。
[utah@utahstu2 ~]$ systemctl start atd #开启服务
[utah@utahstu2 ~]$ systemctl status atd #查看atd状态
[utah@utahstu2 ~]$ systemctl is-enabled atd #查看是否开机启动atd服务,enabled,是。
1.使用at创建计划任务
[utah@utahstu2 ~]$ at 20:46
at>mkdir /tmp/xuegod
at>touch /tmp/xuegod/a.txt
at><EOT>
[utah@utahstu2 ~]$ at -l #查看计划任务
[utah@utahstu2 ~]$ atd #查看计划任务
这个查看,只能看到还没有执行的。如果这个任务已经开始执行或者执行完成了,是看不到的。
4.查看计划任务内容
/var/spool/at/
文件中保存
5.at计划任务的特殊写法
6.删除at计划任务
使用atrm命令删除at计划任务,命令使用语法:atrm 任务编号
。
[root@utahstu2 utah]# at -l
1 Tue Nov 30 15:18:00 2021 a root
[root@utahstu2 utah]# atrm 1
[root@utahstu2 utah]# at -l
16.1.2 crontab定时任务的使用
Crond服务是Linux系统中用来定期检查是否有要执行的工作,如果有要执行的工作便会自动执行该工作。
cron是一个Linux下的定时执行工具,可以在无须人工干预的情况下运行作业。
Linux系统执行任务调度的工作主要是系统周期性所要执行的工作,如更新whatis数据库、更新updatedb数据库、日志定期切割、收集系统状态信息、/tmp目录定期清理等,这些工作都是通过crond服务来完成的。
启动crond服务。
[utah@utahstu2 ~]$ systemctl start crond #开启服务
[utah@utahstu2 ~]$ systemctl enable crond
1.crontab命令参数介绍
crontab命令的参数如下。
(1)crontab -u hr:指定hr用户下的cron服务。
(2)crontab -l:列出当前用户下的cron服务的详细内容。
(3)crontab -u mk -l:列出mk用户下的cron服务的详细内容。
(4)crontab -r:删除cron服务。
(5)crontab -e:编辑cron服务。
例如:
crontab -u root -l
:root用户查看自己的cron计划任务。
crontab -u san -r
:root用户想删除san用户下的cron计划任务。
crontab -e
:编辑cron服务时的写法及格式如图16-1所示。
星期日用0或7表示。
一行对应一个任务,特殊符号的含义如表所示。
符号 | 含义 | 示例 |
---|---|---|
* | 代表取值范围内的数字 | (每) |
/ | 指定时间的间隔频率 | */10、0-23/2 |
- | 代表从某个数字到某个数字 | 8-17 |
, | 分开几个离散的数字 | 6,10-13,20 |
2.创建计划任务
例16.1:每天凌晨2点1分开始备份数据。
[root@utahstu2 utah]# crontab -e
1 2 * * * tar zcvf /opt/grub2.tar.gz /boot/grub2 #编辑模式下输入定时任务
[root@utahstu2 utah]# crontab -l
1 2 * * * tar zcvf /opt/grub2.tar.gz /boot/grub2
[root@utahstu2 utah]#
例16.2:黑客以非root用户添加计划任务。最好使用已经存在的系统用户添加。这里使用bin用户来添加。
[root@utahstu2 utah]# crontab -u bin -e #创建bin用户计划任务,-u选项指定用户。
[root@utahstu2 utah]# crontab -u bin -l #查看bin用户计划任务。
问:如何排查所有用户的计划任务?
注:所有用户的计划任务,都会在/var/spool/cron/
下产生对应的文件。
[root@utahstu2 utah]# ll /var/spool/cron/ #以长格式查看/var/spool/cron/ 目录
所以,后期可以使用此方式排查黑客是否在系统中安装了定时任务。
16.1.3 系统级别的计划任务
系统级别的计划任务。
[root@utahstu2 utah]# ll /etc/crontab
-rw-r--r--. 1 root root 451 6月 10 2014 /etc/crontab
/etc/crontab
是系统任务调度的配置文件:
[root@utahstu2 utah]# vim /etc/crontab
1 SHELL=/bin/bash #指定操作系统使用哪个shell
2 PATH=/sbin:/bin:/usr/sbin:/usr/bin #系统执行命令的搜索路径。
3 MAILTO=root #将执行任务的信息通过邮件发送给某个用户。
4
5 # For details see man 4 crontabs
6
7 # Example of job definition:
8 # .---------------- minute (0 - 59)
9 # | .------------- hour (0 - 23)
10 # | | .---------- day of month (1 - 31)
11 # | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
12 # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
13 # | | | | |
14 # * * * * * user-name command to be executed
也可以直接在/etc/crontab
中添加计划任务。
使用crontab命令的注意事项如下。
① 环境变量的问题。
② 清理邮件日志,如使用重定向>/dev/null 2>&1
。
使用ls
命令列出/etc/cron
目录下的文件和目录。
[root@utahstu2 utah]# ls /etc/cron #按两下Tab键,提示
cron.d/ cron.daily/ cron.deny cron.hourly/ cron.monthly/ crontab cron.weekly/
/etc/cron
目录下文件和目录的作用如下。
cron.d/:系统定期需要自动做的任务,但是又不是按小时、按天、按星期、按月来执行的,那么就放在这个目录下。
cron.deny:控制用户是否能做计划任务的文件。
cron.monthly/:存放每月执行的脚本。
cron.weekly/:存放每周执行的脚本。
cron.daily/:存放每天执行的脚本。
cron.hourly/:存放每小时执行的脚本。
crontab :计划任务的主配置文件,也可添加任务。
16.2 日志的种类和记录的方式
在CentOS 7中,系统日志消息由两个服务负责处理:systemd-journald和rsyslog。
16.2.1 常见日志文件的作用
/var/log
目录是由rsyslog服务维护的,其中存放着一些特定的系统和服务的日志文件,如表1