linux基础--计划任务

计划任务


crond计划任务概述

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

Schedule one-time tasks with at.   一次性调度执行 at
Schedule recurring jobs with cron. 循环调度执行 cron
Schedule recurring system jobs. 所有计划任务执行中的输出都会以邮件的方式发送给指定用户, 除非重定向


//循环调度执行cron 进程每分钟会处理一次计划任务
[root@zyq ~]# systemctl status crond
[root@zyq ~]# ps aux |grep crond
root      1201  0.0  0.0 126264  1640 ?        Ss   11:15   0:00 /usr/sbin/crond -n

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

  • 系统级别的定时任务:

    清理系统缓存
    临时文件清理
    系统信息采集
    日志文件切割

  • 用户级别的定时任务:

    定时同步互联网时间
    定时备份系统配置文件
    定时备份数据库文件

crond配置文件详解

文件              说明
/etc/crontab        //crontab配置文件
/etc/cron.deny      //该文件中所列用户不允许使用crontab命令
/var/spool/cron/*   //所有用户定时文件都存放此目录,文件以用户名命名
/var/log/cron       //定时任务执行后的日志文件,可用来回溯

/etc/cron.deny里面如果有用户名,则表示此文件中所有的用户禁用计划任务,此为黑名单
/etc/cron.allow里面如果有用户名,则表示只有此文件中的用户才可以用计划任务,其他任何用户均不可使用计划任务,此为白名单

[root@zyq ~]# cat /etc/cron.deny 
[root@zyq ~]# echo 'harry' > /etc/cron.deny     #设置‘harry’用户不能只用crontab命令
[root@zyq ~]# cat /etc/cron.deny 
harry
[root@zyq ~]# su - harry 
Last login: Thu Jul  6 11:28:07 CST 2023 on pts/0
[harry@zyq ~]$ crontab -l
You (harry) are not allowed to use this program (crontab)
See crontab(1) for more information        #已被禁止
[harry@zyq ~]$ crontab -e
You (harry) are not allowed to use this program (crontab)
See crontab(1) for more information        #已被禁止
[harry@zyq ~]$


[root@zyq ~]# > /etc/cron.deny 
[root@zyq ~]# cat /etc/cron.deny 
[root@zyq ~]# echo 'harry' > /etc/cron.allow
[root@zyq ~]# cat /etc/cron.allow
harry
[root@zyq ~]# crontab -l
no crontab for root            --->root用户除外,可以使用crontab命令
[root@zyq ~]# su - harry 
Last login: Thu Jul  6 11:37:14 CST 2023 on pts/0
[harry@zyq ~]$ crontab -l
32 11 * * * /bin/bash /tmp/test
[harry@zyq ~]$ exit
logout
[root@zyq ~]# su - wawa        --->除’harry‘以外的用户都被禁止
Last login: Wed Jul  5 18:07:51 CST 2023 on pts/0
[wawa@zyq ~]$ crontab -e
You (wawa) are not allowed to use this program (crontab)
See crontab(1) for more information
[wawa@zyq ~]$

crond计划任务管理

crond任务管理
参数          含义                指定示例
-e      编辑crontab文件内容       crontab -e
-l      查看crontab文件内容       crontab -l
-r      删除crontab文件内容       crontab -r   不建议用此方法,建议crontab -e ‘dd’删除里面需要删除的信息
-u      管理其他用户的计划任务 crontab -u wangqing -l
注意: crontab {-l -e}实际上就是在操作/var/spool/cron/username


[root@zyq ~]# rm -rf /tmp/*
[root@zyq ~]# su - harry 
Last login: Wed Jul  5 18:24:16 CST 2023 on pts/0
[harry@zyq ~]$ vim /tmp/test
[harry@zyq ~]$ cat /tmp/test 
#!/bin/bash

mkdir -p /tmp/$(date '+%Y%m%d')
[harry@zyq ~]$ crontab -e
no crontab for harry - using an empty one
crontab: installing new crontab
[harry@zyq ~]$ crontab -l
32 11 * * * /bin/bash /tmp/test
[harry@zyq ~]$ ls /tmp
test
[harry@zyq ~]$ ls /tmp
20230706  test
[harry@zyq ~]$


crond时间含义
[root@zyq ~]# 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  //星期
# |  |  |  |  |
# *  *  *  *  *   command to be executed

// *    表示任意的(分、时、日、月、周)时间都执行
// -    表示一个时间范围段, 如5-7点
// ,    表示分隔时段, 如6,0,4表示周六、日、四
// */n  表示每隔n单位时间,*/10 每10分钟
crond编写示例
400 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				#写法错误
00 02 14 * 7 command			#写法错误
00 02 14 2 7 command			#写法错误
*  02 * * * command				#写法错误
* * * * *  command				#每分钟执行
* * 14 2 *  command				#每年2月14号执行
*/5 * * * *  command			#每5分钟执行
00 02 * 1,5,8 * command			#每年的1月,5月,8月,的2点执行
00 02 1-8 * *  command			#每年的1至8月的2点执行
crond书写规范

在这里插入图片描述

//1.为计划任务增加必要的注释
[root@zyq ~]# crontab -e -u harry 
crontab: installing new crontab
[root@zyq ~]# crontab -l -u harry 
#写上注释(时间/编写者等),避免被误删。可以是中文注释

32 11 * * * /bin/bash /tmp/test
[root@zyq ~]#

//2.规范计划任务执行脚本存放的路径/scripts/
[root@zyq ~]# crontab -l
##backup www to /backup
30 01 * * * /bin/sh /scripts/www_backup.sh &>/dev/null

#/dev/null 	黑洞设备
#/dev/zero 	吐零设备
[root@zyq ~]# cd /tmp/
[root@zyq tmp]# ls
[root@zyq tmp]# dd if=/dev/zero of=abc bs=1024M count=1
1+0 records in             --->"会通过‘吐0’写入一个可指定大小的文件"
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 6.24925 s, 172 MB/s
[root@zyq tmp]# du -sh abc 
1.0G	abc
[root@zyq tmp]#

//3.执行shell脚本任务前加/bin/sh, 脚本结尾加&>/dev/null
//调试好后应屏蔽debug输出信息,避免产生系统垃圾占用过多inode, 如需输出日志, 可重定向至日志文件
[root@zyq ~]# crontab -l
####backup www to /backup
30 01 * * * /bin/sh /scripts/www_backup.sh &>/tmp/www_backup.log

crond配置编写实例

[root@wangqing ~]# crontab -e

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

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



#注意:
1.我们所有的crond服务是运行的程序。而crontab命令用户用来设置定时规则的命令。
2.crond服务是企业生产工作中常用的重要服务,at很少使用,可以忽略。
3.几乎每个服务器都会用到crond服务。
练习

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

a. 00 13,20 * 1-5 * backup.sh
b. 0 13,20 1,5 * * backup.sh
c. * 13,20 * * 1-5 backup.sh
d. 00 13,20 * * 1-5 backup.sh

说明以上答案每一行是什么含义

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

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

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

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

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

#说明以上答案每一行是什么含义
   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:  #每周1-5的下午一点到晚上8点的每分钟执行
   D、00 13,20 * * 1-5 backup.sh: #每周1-5的每天下午一点和晚上8点执行

#新建/scripts/httpd.sh文件,并让/scripts/httpd.sh脚本在每天的00:10分执行
[root@zyq ~]# mkdir -p /scripts/httpd.sh
[root@zyq ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@zyq ~]# crontab -l
10 00 * * * /bin/bash -x /scripts/httpd.sh &> /dev/null

#新建/backup目录,每周一下午5:50将/backup目录下的所有文件打包成 backup.tar.gz
[root@zyq ~]# mkdir backup
[root@zyq ~]# crontab -e
crontab: installing new crontab
[root@zyq ~]# crontab -l
50 5 * * 1  /usr/bin/tar  -zcf  backup.tar.gz  /root/backup

#写一个定时任务,每天0点5分把/var/log/nginx下7天前的文件转移到/backup/2018_xx_xx的目录中
[root@zyq ~]# crontab -e
crontab: installing new crontab
[root@zyq ~]# crontab -l
05 00 * * *  /usr/bin/find  /var/log/nginx -name "*" -mtime +7 -type f -exec mv {} /backup/$(date +%y2018 -%m -%d)/ \;

#系统脚本/scripts/which.sh,如何定时每隔7分钟执行一次?
[root@zyq ~]# crontab -e
crontab: installing new crontab
[root@zyq ~]# crontab -l
*/7 * * * * /bin/bash -x /scripts/which.sh

#如何不小心删除了/var/spool/cron/root文件,该如何恢复。
[root@zyq ~]#  cat  /var/log/cron* | grep CMD | head

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值