实验环境为我的desktop虚拟机
(1)at命令用于定时任务
[root@localhost ~]# at 23:37设定任务执行时间at> rm -fr /mnt/*任务动作at> (直接ctrl+d发起任务就会显示 )用ctrl+D发起任务步骤一:采用watch -n 1 ls -l /mnt/监控命令,监控该过程
步骤二:先用date查看一下desktop主机的当前时间
步骤三:at 时间 回车,此时就可以建立任务了 ctrl+d退出
步骤四:利用at命令操作定时任务
(2)at命令用于延时任务
步骤一:设定延时任务
步骤二:查看延时任务是否完成
步骤三:at now+1min rm -rf *
(3)at任务的黑白名单
注意:
(1)系统控制crontab的服务
crond.service##当程序开启时定时任务生效
(2)crontab 命令后面所跟的参数
(3)crontab命令的具体格式
crontab -e -u root为root用户创建计划任务crontab -l -u root列出root用户的计划任务crontab -r -u root删除root用户的计划任务(4)crontab 时间表示方式
分钟 时 日 月 星期几具体时间* * * * *每分钟*/2 * * * *每两分钟*/2 09-17 * * *早7-晚5每两分钟*/2 */2 * * *每隔2小时每两分钟*/2 09-17 3,5 15月的每个周四以及三月1号*/2 09-17 * * 5每周周五早9晚5注意:最后的星期几与日月是或的关系
(5)实验步骤
步骤一:systemctl status crond.service,systemctl start crond.service(这个服务必须开启)
步骤二:crontab -e -u root 为root用户创建定时任务
步骤三:出来一个vim编辑,然后写入要定时的任务格式如上
步骤四:crontab -l -u root列出root用户的定时任务
步骤五:cat /var/spool/cron/root在这个文件里面可以看到定时任务
步骤六:crontab -r -u root 删除root用户的定时任务
步骤六:rm -rf /var/spool/cron/root
(1)* * * * * root run-parts /westos 定时任务多,将任务放在一个目录下,写脚本
(2)* * * * * root rm -rf /mnt/*定时任务少,直接将任务写在后面
vim /etc/cron.d/file* * * * * usernameaction * * * * * root rm -fr /mnt/*动作较少时直接写用户名字和动作
(1)cd /usr/lib/tmpfiles.d/在这个目录下创建临时文件
vim westos.conf 创建临时文件d /mnt/westos 777 root root 8s (这个目录是没有的)目录 目录名称 权限 所有人 所有组 生命周期 这个目录的属性生命周期是目录下面的文件的生命周期,目录的作用是作用于文件
(2)systemd-tmpfiles --create /usr/lib/tmpfiles.d/* 创建(自动建立/mnt/westos/)
(3)systemd-tmpfiles --clean /usr/lib/tmpfiles.d/* 清空(清空/mnt/westos/这个目录下面的东西)
系统不同的版本使用说明不一样,要会用man手册查看
(1)* * * * * root run-parts /westos 定时任务多,将任务放在一个目录下,写脚本
(2)* * * * * root rm -rf /mnt/*定时任务少,直接将任务写在后面
vim /etc/cron.d/file* * * * * usernameaction * * * * * root rm -fr /mnt/*动作较少时直接写用户名字和动作
(1)cd /usr/lib/tmpfiles.d/在这个目录下创建临时文件
vim westos.conf 创建临时文件d /mnt/westos 777 root root 8s (这个目录是没有的)目录 目录名称 权限 所有人 所有组 生命周期 这个目录的属性生命周期是目录下面的文件的生命周期,目录的作用是作用于文件
(2)systemd-tmpfiles --create /usr/lib/tmpfiles.d/* 创建(自动建立/mnt/westos/)
(3)systemd-tmpfiles --clean /usr/lib/tmpfiles.d/* 清空(清空/mnt/westos/这个目录下面的东西)
系统不同的版本使用说明不一样,要会用man手册查看