跳转至

crond计划任务

Crond简介

定时任务: 指定某个日期或时间周期性的执行指令
参考文档链接: https://zhuanlan.zhihu.com/p/338375307

Crond是Linux系统中用来定期执行命令或脚本的一种服务软件, 一般情况下, 我们安装完CentOS操作系统之后, 默认便会启动Crond任务调度服务...

Crond服务会定期(默认每分钟检查一次)检查系统中是否有要执行的任务工作, 若有, 便会根据其预先设定的定时任务规则自动执行该定时任务工作...

[root@localhost ~]# ps -ef | grep crond | grep -v grep
root       1081      1  0 09:03 ?        00:00:00 /usr/sbin/crond -n

Crond使用场景

定期备份数据, 定期执行脚本程序

系统级别的定时任务
        临时文件清理例/tmp和/var/tmp等、系统信息采集、日志文件切割

用户级别的定时任务
        定时向互联网同步时间、定时备份系统配置文件、定时备份数据库的数据

#!bin/bash

mem_total=`free -m | awk 'NR==2 {print $2}'`
mem_used=`free -m | awk 'NR==2 {print $3}'`
mem_free=`free -m | awk 'NR==2 {print $4}'`
mem_percent=`free -m | awk 'NR==2 {printf "%.2f%\n", ($3/$2)*100}'`

echo "---- mem check info ----"
echo "总内存(M): $mem_total"
echo "已使用(M): $mem_used"
echo "剩余量(M): $mem_free"
echo "使用率: $mem_percent"


[root@localhost ~]# bash mem_check.sh
---- mem check info ----
总内存(M): 972
已使用(M): 208
剩余量(M): 425
使用率: 21.40%

# -- 今天备份昨天的
cp /var/log/messages /var/log/messages-$(date +%Y%m%d -d '-1 day')

定时任务管理

/etc/crontab

分钟 小时 日 月 周

*        表示任意的(分时日月周)都会运行
-        表示一个时间范围, eg 5-7点
,        表示分隔时段, 如6,0,4表示周六、日、四
/1        表示每隔n单位时间, 如*/10 每10分钟

[root@localhost ~]# 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 创建定时任务
* * * * * echo "hello world" >> /tmp/1.txt      # -- 每分钟往1.txt文件写入一次"hello world"

编写举例

分钟 小时 日 月 周

00 02 * * *       ## -- 每天的凌晨2点整执行

00 02 1 * *       ## -- 每月的1日的凌晨2点整执行

00 02 14 2 *      ## -- 每年的2月14日凌晨2点执行

00 02 * * 7       ## -- 每周天的凌晨2点整执行

00 02 * 6 5       ## -- 每年的6月周五凌晨2点执行

00 02 14 * 7      ## -- 每月14日或每周日的凌晨2点都执行

00 02 14 2 7      ## -- 每年的2月14日或每年2月的周天的凌晨2点执行

*/10 02 * * *     ## -- 每天凌晨2点,每隔10分钟执行一次

* * * * *         ## -- 每分钟都执行

00 00 14 2 *      ## -- 每年2月14日的凌晨执行命令

*/5 * * * *       ## -- 每隔5分钟执行一次

00 02 * 1,5,8 *   ## -- 每年的1月5月8月凌晨2点执行

00 02 1-8 * *     ## -- 每月1号到8号凌晨2点执行

00 21 * * *       ## -- 每天晚上21:00执行

45 4 1,10,22 * *  ## -- 每月1、10、22日的4:45执行

45 4 1-10 * *     ## -- 每月1到10日的4:45执行

3,15 8-11 */2 * * ## -- 每隔两天的上午8点到11点的第3和第15分钟执行

0 23-7/2 * * *    ## -- 晚上11点到早上7点之间,每隔两小时执行

15 21 * * 1-5     ## -- 周一到周五每天晚上21:15执行

命令选项

-e      编辑定时任务
-l      查看定时任务
-r      删除定时任务
-u      指定其他用户

[root@localhost ~]# crontab -l
* * * * * echo "hello world" >> /tmp/1.txt

定时任务实践

▲ 使用root用户每5分钟执行一次时间同步.

## -- step1:安装nptdate包
yum install -y ntpdate

## -- step2:测试时间同步命令 (返回结果没有报错即可)
[root@localhost ~]# ntpdate ntp.aliyun.com
30 Aug 20:45:19 ntpdate[11577]: adjust time server 203.107.6.88 offset 0.178911 sec

## -- step3:确定命令所在目录 在 /etc/crontab文件的PATH字段所包含的目录下
## -- 不在的话,编写定时任务时候,可用绝对路径解决!!
[root@localhost ~]# cat /etc/crontab | grep PATH
PATH=/sbin:/bin:/usr/sbin:/usr/bin
[root@localhost ~]# which ntpdate
/usr/sbin/ntpdate

## -- step4:配置并查看定时任务
[root@localhost ~]# crontab -e
crontab: installing new crontab
[root@localhost ~]# crontab -l
# -- 每5分钟执行一次时间同步 最好使用绝对路径 /usr/sbin/ntpdate ntp.aliyun.com
*/5 * * * * ntpdate ntp.aliyun.com

▲ 备份案例
需求:
     提前准备备份文件目录,命名规则: /backup/web01_172.16.150.131_2022-08-30
     打包备份文件: 名字 2022-08-30_hostname_etc.tar.gz
     验证文件是否完整
     将验证结果发送到邮箱
     存放备份内容的目录要求只保留3天的数据
     每天的凌晨3点50执行该备份

脚本文件 bei.sh 的内容如下:

#!bin/bash

hostname=`hostname`
ip=`hostname -I|awk '{print $1}'`
date=`date +%F`

mkdir -p  /backup/${hostname}_${ip}_${date}
tar zcf /backup/${hostname}_${ip}_${date}/${date}_${hostname}_etc.tar.gz /etc
md5sum /backup/${hostname}_${ip}_${date}/${date}_${hostname}_etc.tar.gz > /backup/flag.txt
md5sum -c /backup/flag.txt > /backup/mail.txt
# -- 将验证结果发送到邮箱 此过程略.
find /backup/ -type f -mtime +3 | xargs rm -rf

写入定时任务:

crontab -e
# -- 写入命令   50 3 * * * /bin/bash ~/bei.sh

定时任务编写

思路

1.手动执行命令,然后保留执行成功的结果
2.编写脚本
  脚本需要统一路径/scripts
  脚本内容复制执行成功的命令(减少每个环节出错几率)
  脚本内容尽可能的优化,使用一些变量或简单的判断语句
  脚本执行的输出信息可以重定向至其它位置保留或写入/dev/null
3.执行脚本
  使用bash命令执行,防止脚本没有增加执行权限(/usr/bin/bash /bin/bash)
  执行脚本成功后,复制该执行的命令,以便写入cron
4.编写定时任务
  加上必要的注释信息, 人、时间、任务
  设定定时任务执行的周期
  粘贴执行脚本的命令(不要手敲)
5.调试定时任务
  增加任务频率测试
  检查环境变量问题(最好在脚本中重新定义环境变量PATH)
  检查crond服务日志  /var/log/cron

注意点

1> 定时任务规则配置一定要加注释
2> 定时任务里面尽量使用脚本
3> 运行脚本习惯性的使用绝对路径 用户的定时任务中的文件默认存放在当前用户的家目录
   比如: /bin/bash ~/bei.sh bash命令用的绝对路径
4> 脚本或者命令的执行结果尽量将有用的定义到指定日志,没用的定义到空
5> 避免不必要的输出 比如: tar -zcvf etc.tar.gz /etc  f选项会输出打包的过程,没必要加这个选项
6> 打包压缩命令使用绝对路径
7> 系统与命令位置有关的环境变量问题,建议脚本中重新定义环境变量PATH
8> 定时任务中date命令的百分号需转义才能使用