Linux-计划任务管理

Linux-计划任务管理

crond计划任务概述

什么是计划任务,计划任务类似于我们平时生活中的闹钟。
Linux系统的计划任务服务crond可以满足周期性执行任务的需求。
crond进程每分钟会处理一次计划任务, 计划任务主要是做一些周期性的任务目前最主要的用途是定时备份数据

//查看crond计划任务服务状态(设置计划任务时检查服务是否开启)
[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 Sat 2020-12-05 21:54:10 CST; 13min ago
 Main PID: 1107 (crond)
    Tasks: 1 (limit: 4842)
   Memory: 1.9M
   CGroup: /system.slice/crond.service
           └─1107 /usr/sbin/crond -n

Dec 05 21:54:10 localhost.localdomain systemd[1]: Started Command Scheduler.
Dec 05 21:54:10 localhost.localdomain crond[1107]: (CRON) STARTUP (1.5.2)
Dec 05 21:54:10 localhost.localdomain crond[1107]: (CRON) INFO (Syslog will be used i
nstead of sendmai>
Dec 05 21:54:10 localhost.localdomain crond[1107]: (CRON) INFO (RANDOM_DELAY will be scaled with facto>
Dec 05 21:54:11 localhost.localdomain crond[1107]: (CRON) INFO (running with inotify support)
Dec 05 22:01:01 localhost.localdomain CROND[5233]: (root) CMD (run-parts /etc/cron.hourly)
Dec 05 22:01:01 localhost.localdomain anacron[5242]: Anacron started on 2020-12-05
Dec 05 22:01:01 localhost.localdomain anacron[5242]: Normal exit (0 jobs run)

计划任务分为以下两种情况:

系统级别的定时任务:
  • 清理系统缓存
  • 临时文件清理
  • 系统信息采集
  • 日志文件切割
用户级别的定时任务:
  • 定时同步互联网时间
  • 定时备份系统配置文件
  • 定时备份数据库文件

crond配置文件详解

文件 说明
/etc/crontab crontab配置文件
/etc/cron.deny 该文件中所列用户不允许使用crontab命令
/var/spool/cron/* 所有用户定时文件都存放此目录,文件以用户名命名
/var/log/cron 定时任务执行后的日志文件,可用来回溯
查看系统计划任务
[root@localhost ~]# ll /etc/cron*
-rw-r--r--. 1 root root   0 Jun 13  2019 /etc/cron.deny	 
											//写到cron.deny中的用户不能使用计划任务(黑名单)
-rw-r--r--. 1 root root 451 Aug 12  2018 /etc/crontab	
											//只有写到crontab中的用户才能用计划任务(白名单)

/etc/cron.d:
total 4
-rw-r--r--. 1 root root 128 Jun 13  2019 0hourly	

/etc/cron.daily:									//每天都会执行的任务(日志滚动用脚本)
total 8
-rwxr-xr-x. 1 root root 189 Jan  4  2018 logrotate	
-rwx------. 1 root root 653 Apr  3  2020 rhsmd

/etc/cron.hourly:									//每小时会执行的任务
total 4
-rwxr-xr-x. 1 root root 575 Jun 13  2019 0anacron

/etc/cron.monthly:									//每个月会执行的任务
total 0

/etc/cron.weekly:									//每周会执行的任务
total 0

crond计划任务管理

参数 含义
crontab -e 编辑crontab文件内容
crontab -l 查看crontab文件内容
crontab -r 删除crontab文件(禁止使用)
crontab -u tom -l 管理其他用户的计划任务(只能管理员使用)

crond时间含义

 .---------------- 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  //星期
 |  |  |  |  |
 *  *  *  *  *   command

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

crond编写示例

00 02 * * * command		//每天凌晨2点整执行
00 02 1 * * command		//每个月1号凌晨2点整执行
00 02 14 2 * command	        //每年的2月14号凌晨2点整执行
00 02 * * 7 command		//每个周日凌晨2点整执行
00 02 * 6 5 command		//每年的六月份的每周五凌晨2点整执行
00 02 14 * 7 command	        //每个月的14号凌晨2点整 或 每周日凌晨2点整执行
00 02 14 2 7 command	        //每年2月的14号凌晨2点整 或 每周日凌晨2点整执行
* 02 * * * command		//错误写法,没有指定准确时间
* * * * * command		//每分钟执行一次
* * 14 2 * command		//错误写法,没有指定准确时间
*/5 * * * * command		//每5分钟执行一次
00 02 * 1,5,8 * command	        //每年的1月,5月,8月凌晨2点整执行
00 02 1-8 * * command	        //每个月的1号到8号凌晨2点整执行

crond书写规范

在公司工作写计划任务时,需要添加相关注释:

  • 计划任务是谁写的
  • 计划任务是什么时候写的
  • 计划任务是干什么用的

计划任务用的命令和脚本必须要使用绝对路径;

执行计划任务的时候可能会有报错,所以需要记录日志文件

#每天凌晨0点切割nginx日志
00 00 * * * /bin/bash -x /scripts/cut_nginx.sh &> /scripts/log/nginx.log

#每天凌晨2点备份数据库
00 02 * * * /bin/bash -x /scripts/dump_sql.sh &>/scripts/log/mysql.log

crond计划任务调试

  1. 调整任务每分钟执行,检测任务是否正常
  2. 将脚本执行输出写入指定日志文件,观察日志内容是否正常
  3. 命令和脚本使用绝对路径,防止无法找到导致执行故障
  4. 查看/var/log/cron日志进行调试

建议: 将需要定期执行的任务写入脚本中, 建立/scripts目录统一存放脚本, 脚本中命令必须使用绝对路径,手动执行脚本检测输出是否正常, 然后将脚本加入计划任务测试, 测试后无问题将脚本输出写入对应的日志文件中即可。

//避免写出错误无用的命令,导致没有写入日志
[root@localhost ~]# echo "yqh" >> /tmp/test.log &>/dev/null
[root@localhost ~]# cat /tmp/test.log 
[root@localhost ~]# echo "yqh" &>/dev/null >> /tmp/test.log
[root@localhost ~]# cat /tmp/test.log 
yqh

//查看cron日志
[root@localhost ~]# tail /var/log/cron
Dec  5 23:01:01 localhost anacron[5337]: Normal exit (0 jobs run)
Dec  5 23:01:01 localhost run-parts[5328]: (/etc/cron.hourly) finished 0anacron
Dec  5 23:51:56 localhost crontab[5368]: (root) LIST (root)
Dec  5 23:52:08 localhost crontab[5369]: (root) BEGIN EDIT (root)
Dec  6 00:01:01 localhost CROND[5381]: (root) CMD (run-parts /etc/cron.hourly)
Dec  6 00:01:01 localhost run-parts[5381]: (/etc/cron.hourly) starting 0anacron
Dec  6 00:01:01 localhost anacron[5390]: Anacron started on 2020-12-06
Dec  6 00:01:01 localhost anacron[5390]: Normal exit (0 jobs run)
Dec  6 00:01:01 localhost run-parts[5381]: (/etc/cron.hourly) finished 0anacron
Dec  6 00:10:18 localhost crontab[5369]: (root) END EDIT (root)

练习题

选择题:

1.在linux系统中备份脚本backup.sh需要在每周1-5的每天下午1点和晚上8点执行,下列哪个cron命令可以完成(D)

A. 00 13,20 * 1-5 * backup.sh //每年的1月到5月的每天下午1点和晚上8点执行
B. 0 13,20 1,5 * * backup.sh //每个月的1号和5号的下午1点和晚上8点执行
C. * 13,20 * * 1-5 backup.sh //错误写法,没有指定准确时间
D. 00 13,20 * * 1-5 backup.sh //每个月周一到周五的下午1点和晚上8点执行

操作题

2.新建/scripts/httpd.sh文件,并让/scripts/httpd.sh脚本在每天的00:10分执行

[root@localhost ~]# mkdir /scripts
[root@localhost ~]# cd /scripts/
[root@localhost scripts]# touch httpd.sh
no crontab for root - using an empty one
crontab: installing new crontab
[root@localhost scripts]# crontab -l
## yuqinghao 2020/12/06 bash -x /scripts/httpd.sh
10 00 * * * /bin/bash -x /scripts/httpd.sh &> /scripts/log/httpd.log

3.新建/backup目录,每周一下午5:50将/backup目录下的所有文件打包成backup.tar.gz

[root@localhost ~]# mkdir /backup
[root@localhost ~]# which tar
/usr/bin/tar
[root@localhost ~]# crontab -e
crontab: installing new crontab
[root@localhost ~]# crontab -l
## yuqinghao 2020/12/06 tar -zcf /backup/*
50 17 * * 1 /usr/bin/tar -zcf /backup/* &> /scripts/log/tar.log

4.写一个定时任务,每天0点5分把/var/log/nginx下7天前的文件转移到/backup/2018_xx_xx的目录中

[root@localhost ~]# which mv
alias mv='mv -i'
	/usr/bin/mv
[root@localhost ~]# crontab -e
crontab: installing new crontab
[root@localhost ~]# crontab -l
## yuqinghao 2020/12/06 mv /var/log/nginx/* /backup/2018_xx_xx
05 00 */7 * * /usr/bin/mv /var/log/nginx/* /backup/2018_xx_xx &> /scripts/log/nginx.log

5.系统脚本/scripts/which.sh,如何定时每隔7分钟执行一次?

[root@localhost ~]# crontab -e
crontab: installing new crontab
[root@localhost ~]# crontab -l
## yuqinghao 2020/12/06 bash -x /scripts/which.sh
*/7 * * * * /bin/bash -x /scripts/which.sh &> /scripts/log/which.log

6.如何不小心删除了/var/spool/cron/root文件,该如何恢复?

//备份
[root@localhost ~]# crontab -l > /opt/root
[root@localhost ~]# cat /opt/root
## yuqinghao 2020/12/06 bash -x /scripts/httpd.sh
10 00 * * * /bin/bash -x /scripts/httpd.sh &> /scripts/log/httpd.log

## yuqinghao 2020/12/06 tar -zcf /backup/*
50 17 * * 1 /usr/bin/tar -zcf /backup/* &> /scripts/log/tar.log

## yuqinghao 2020/12/06 /mv /var/log/nginx/* /backup/2018_xx_xx
05 00 */7 * * /usr/bin/mv /var/log/nginx/* /backup/2018_xx_xx &> /scripts/log/nginx.log

## yuqinghao 2020/12/06 bash -x /scripts/which.sh
*/7 * * * * /bin/bash -x /scripts/which.sh &> /scripts/log/which.log
[root@localhost ~]# crontab -l
## yuqinghao 2020/12/06 bash -x /scripts/httpd.sh
10 00 * * * /bin/bash -x /scripts/httpd.sh &> /scripts/log/httpd.log

## yuqinghao 2020/12/06 tar -zcf /backup/*
50 17 * * 1 /usr/bin/tar -zcf /backup/* &> /scripts/log/tar.log

## yuqinghao 2020/12/06 /mv /var/log/nginx/* /backup/2018_xx_xx
05 00 */7 * * /usr/bin/mv /var/log/nginx/* /backup/2018_xx_xx &> /scripts/log/nginx.log

## yuqinghao 2020/12/06 bash -x /scripts/which.sh
*/7 * * * * /bin/bash -x /scripts/which.sh &> /scripts/log/which.log

//不小心删了
[root@localhost ~]# crontab -r
[root@localhost ~]# crontab -l
no crontab for root

//恢复
[root@localhost cron]# cp /opt/root /var/spool/cron/
[root@localhost ~]# crontab -l
## yuqinghao 2020/12/06 bash -x /scripts/httpd.sh
10 00 * * * /bin/bash -x /scripts/httpd.sh &> /scripts/log/httpd.log

## yuqinghao 2020/12/06 tar -zcf /backup/*
50 17 * * 1 /usr/bin/tar -zcf /backup/* &> /scripts/log/tar.log

## yuqinghao 2020/12/06 /mv /var/log/nginx/* /backup/2018_xx_xx
05 00 */7 * * /usr/bin/mv /var/log/nginx/* /backup/2018_xx_xx &> /scripts/log/nginx.log

## yuqinghao 2020/12/06 bash -x /scripts/which.sh
*/7 * * * * /bin/bash -x /scripts/which.sh &> /scripts/log/which.log
原文地址:https://www.cnblogs.com/yuqinghao/p/14135977.html