计划任务管理

计划任务管理


通过 crontab 和 at 这两个东西来实现这些功能的
计划任务的作用:是做一些周期性的任务,在生产中的主要用来定期备份数据
CROND:这个守护进程是为了周期性执行任务或处理等待事件而存在
任务调度分两种:系统任务调度,用户任务调度

计划任务的安排方式分两种:
一种是定时性的,也就是例行。就是每隔一定的周期就要重复来做这个事情
一种是突发性的,就是这次做完了这个事,就没有下一次了,临时决定,只执行一次的任务

at 和 crontab 这两个命令:

at:它是一个可以处理仅执行一次就结束的指令
crontab:它是会把你指定的工作或任务,比如:脚本等,按照你设定的周期一直循环执行下去

at 计划任务的使用
语法格式: at 时间 ;服务:atd  
[root@caicai ~]# yum -y install at  # 没有需要安装一下
[root@caicai ~]# systemctl start atd #开启 atd 服务
[root@caicai ~]# systemctl status atd #查看 atd 服务状态
[root@caicai ~]# systemctl is-enabled atd  #查看是否开始开机启动服务,如果弹出 enabled,说明开机启动此服务
在 Centos6 查看开机启动服务:
[root@caicai ~]# chkconfig --list | grep atd #此命令在 centos7 上不能执行
在 Centos7 之后的系统查看是否开机启动:
[root@caicai ~]# systemctl list-unit-files

实战-使用 at 创建计划任务
[root@caicai ~]# date #查看系统时间

[root@caicai ~]# at 20:46 #注意:如果是上午时间,后面加上 am,9:20am

at> mkdir /tmp/catcai #输入你要执行的命令

at> touch /tmp/caiai/a.txt
#结束:ctrl+d

[root@caicai ~]# at -l #查看计划任务

[root@caicai ~]# atq #查看计划任务

检查 at 计划任务运行结果:
[root@caicai ~]# ls /tmp/caicai/
a.txt
如果正在执行命令,ctrl+D ,按成 ctrl+S 会怎么样? 尤其是使用 vim 保存,按成 ctrl+s 
解决: ctrl+s 在 linux 下是锁定屏幕显示的意思,这时整个界面被锁定,不能进行正常输入。使用ctrl+q 来解除锁定,

查看和删除 at 将要执行的计划任务

这个查看,只能看到还没有执行的。如果这个任务已经开始执行或者执行完成了,是看不到的
[root@caicai ~]# at -l 5 

[root@caicai ~]# at -c 5 #-c:打印任务的内容到标准输出, 查看 5 号计划任务具体内容

查看定时任务内容
[root@caicai ~]# ls /var/spool/at/


[root@caicai ~]# tail -10 /var/spool/at/a0000501845084
at 计划任务的特殊写法

[root@ caicai ~]# at 20:00 2030-12-29 在某天
[root@ caicai ~]# at now +10min 在 10 分钟后执行
[root@ caicai ~]# at 17:00 tomorrow 明天下午 5 点执行
[root@caicai ~]# at 6:00 pm +3 days 在 3 天以后的下午 6 点执行
[root@caicai ~]# at 23:00 < /root/a.txt 把 a.txt 的内容输入给他也可以

vim a.txt 
mkdir /opt/test
touch /opt/test/test.txt


删除 at 计划任务
语法: atrm 任务编号

[root@caicai ~]# at -l 

[root@caicai ~]# atrm 3 5
[root@caicai ~]# at -l 3 


cron 命令参数介绍
crontab 的参数:
crontab -l #列出当前用户下的 cron 服务的详细内容
crontab -u user1 -l #列出指定用户 user1 下的 cron 服务的详细内容
crontab -r #删除 cron 服务
crontab -e #编辑 cron 服务
例如:
crontab -u root -l # root 查看自己的 cron 计划任务
crontab -u user1 -r # root 想删除 user1 的 cron 计划任务

crontab -e 编辑时的语法
星期日用 0 或 7 表示
一行对应一个任务,特殊符号的含义: 
* 代表取值范围内的数字 (任意/每) 
/ 指定时间的间隔频率 */10 0-23/2 放在小时下(在 0-23 点之间,每隔 2 小时执行一 次)- 代表从某个数字到某个数字 8-17 8 到 17 之间执行
, 分开几个离散的数字 6,10-13,20 6 执行,10 到 13 之间执行,20 执行

创建计划任务
例 1:每天凌晨 2 点 1 分开始备份数据
[root@caicai spool]# crontab -e #添加计划任务
1 2 * * * tar zcvf /opt/grub2.tar.gz /boot/grub2 
[root@caicai ~]# crontab -l #查看
例 2:以非 root 用户添加计划任务。 最好使用已经存在系统用户添加。这里使用 bin 用户来添加
[root@caicai ~]# crontab -u bin -e
*/1 * * * * echo "aaaaaaa" >> /tmp/bin.txt
1 * * * * 每小时第 1 分钟
/1 * * * * 每分钟


注:所有用户的计划任务,都会在/var/spool/cron/下产生对应的文件
[root@caicai ~]# ll /var/spool/cron/ 

-rw------- 1 root root 42 Nov 12 10:11 bin
-rw------- 1 root root 19 Nov 12 10:06 root
所以后期可以使用这一招排查,黑客是否在你的机器中安装了定时任务

系统级别的计划任务
[root@caicai etc]# ll /etc/crontab
-rw-r--r--. 1 root root 451 Dec 28 2013 /etc/crontab 
这个是系统任务调度的配置文件
[root@caicai etc]# vim /etc/crontab
SHELL=/bin/bash #指定操作系统使用哪个 shell
PATH=/sbin:/bin:/usr/sbin:/usr/bin #系统执行命令的搜索路径
MAILTO=root #将执行任务的信息通过邮件发送给 xx 用户
# 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

也可以直接在/etc/crontab 中添加计划任务
使用 crontab 命令的注意事项:

环境变量的问题
清理你的邮件日志 ,比如使用重定向 >/dev/null 2>&1

[root@caicai bin]# ls /etc/cron #按两下 tab 键
cron.d/ cron.deny cron.monthly/ cron.weekly/
cron.daily/ cron.hourly/ crontab 
注: 
cron.d/ #是系统自动定期需要做的任务,但是又不是按小时,按天,按星期,按月来执行的,那么就放在这个目录下面。
cron.deny #控制用户是否能做计划任务的文件;
cron.monthly/ #每月执行的脚本;
cron.weekly/ #每周执行的脚本;
cron.daily/ #每天执行的脚本;
cron.hourly/ #每小时执行的脚本;
crontab #主配置文件 也可添加任务;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值