Linux定时任务

计划任务基本概述

crond就是计划任务。

计划任务主要分为以下两种使用情况:1,.系统级别的定时任务: 临时文件清理、系统信息采集、日志文件切割.2,用户级别的定时任务: 定时向互联网同步时间、定时备份系统配置文件、定时备份数据库的数据.

计划任务时间管理

  • 1,Crontab配置文件记录了时间周期的含义
[root@carol~]# vim /etc/crontab
SHELL=/bin/bash                     #执行命令的解释器
PATH=/sbin:/bin:/usr/sbin:/usr/bin  #环境变量
MAILTO=root                         #邮件发给谁
# 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  #星期
# |  |  |  |  |
# *  *  *  *  *   command to be executed

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

00 02 * * * ls      #每天的凌晨2点整执行
00 02 1 * * ls      #每月的1日的凌晨2点整执行
00 02 14 2 * ls     #每年的2月14日凌晨2点执行
00 02 * * 7 ls      #每周天的凌晨2点整执行
00 02 * 6 5 ls      #每年的6月周五凌晨2点执行
00 02 14 * 7 ls     #每月14日或每周日的凌晨2点都执行
00 02 14 2 7 ls     #每年的2月14日或每年2月的周天的凌晨2点执行   
*/10  02 * * * ls   #每天凌晨2点,每隔10分钟执行一次
* * * * *  ls       #每分钟都执行
00 00 14 2 *  ls    #每年2月14日的凌晨执行命令 
*/5 * * * *  ls     #每隔5分钟执行一次
00 02 * 1,5,8 * ls  #每年的1月5月8月凌晨2点执行
00 02 1-8 * *  ls    #每月1号到8号凌晨2点执行
0 21 * * * ls       #每天晚上21:00执行
45 4 1,10,22 * * ls #每月1、10、22日的4:45执行
45 4 1-10 * * l     #每月1到10日的4:45执行
3,15 8-11 */2 * * ls #每隔两天的上午8点到11点的第3和第15分钟执行
0 23-7/1 * * * ls   #晚上11点到早上7点之间,每隔一小时执行
15 21 * * 1-5 ls    #周一到周五每天晚上21:15执行

  • 3.使用crontab编写cron定时任务
参数 含义
-e 编辑定时任务
-l 查看定时任务
-r 删除定时任务
-u 指定其他用户

计划任务示例

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

#1.如何同步时间
[root@carol~]# ntpdate time.windows.com &>/dev/null
#2.配置定时任务
[root@carol~]# crontab -e -u root
[root@carol~]# crontab -l -u root
*/5 * * * * ntpdate time.windows.com &>/dev/null

2.每天的下午3,5点,每隔半小时执行一次sync命令

[root@carol~]# crontab -l
*/30 15,17 * * * sync &>/dev/null

3.案例:每天凌晨3点做一次备份?备份/etc/目录到/backup下面

  1. 将备份命令写入一个脚本中
  2. 每天备份文件名要求格式: 2019-05-01_hostname_etc.tar.gz
  3. 在执行计划任务时,不要输出任务信息
  4. 存放备份内容的目录要求只保留三天的数据
#1.实现如上备份需求
[root@carol ~]# mkdir /backup
[root@carol ~]# tar zcf $(date +%F)_$(hostname)_etc.tar.gz /etc
[root@carol ~]# find /backup -name “*.tar.gz” -mtime +3 -exec rm -f {}\;

#2.将命令写入至一个文件中
[root@carol ~]# vim /root/back.sh
mkdir /backup
tar zcf $(date +%F)_$(hostname)_etc.tar.gz /etc
find /backup -name “*.tar.gz” -mtime +3 -exec rm -f {}\;

#3.配置定时任务
[root@carol ~]# crontab -l
00 03 * * * bash /root/back.sh  &>/dev/null

#3.备份脚本
  • 4.crond注意的事项
  1. 给定时任务注释
  2. 将需要定期执行的任务写入Shell脚本中,避免直接使用命令无法执行的情况tar date
  3. 定时任务的结尾一定要有&>/dev/null或者将结果追加重定向>>/tmp/date.log文件
  4. 注意有些命令是无法成功执行的 echo "123" >>/tmp/test.log &>/dev/null
  5. 如果一定要是用命令,命令必须使用绝对路径
  • 5.crond如何备份
  1. 通过查找/var/log/cron中执行的记录,去推算任务执行的时间
  2. 定时的备份/var/spool/cron/
  • 6.crond如何拒绝某个用户使用
#1.使用root将需要拒绝的用户加入/etc/cron.deny
[root@carol~]# echo "xuliangwei" >> /etc/cron.deny

#2.登陆该普通用户,测试是否能编写定时任务
[oldboy@carol~]$ crontab -e
You (carol) are not allowed to use this program (crontab)
See crontab(1) for more information

调试计划任务

  • 1.crond调试
  1. 调整任务每分钟执行的频率, 以便做后续的调试。
  2. 如果使用cron运行脚本,请将脚本执行的结果写入指定日志文件, 观察日志内容是否正常。
  3. 命令使用绝对路径, 防止无法找到命令导致定时任务执行产生故障。
  4. 通过查看/var/log/cron日志,以便检查我们执行的结果,方便进行调试。
  • 2.crond编写思路
posted @ 2021-07-25 16:10  Carol-z  阅读(140)  评论(0编辑  收藏  举报