系统定时任务及延时任务

####系统延时任务及定时任务##
1.系统延时任务
[root@localhost  ~]#at 23:37     ##设定任务执行时间
at>  rm  -fr  /mnt/*                 ##任务动作
at>  ctrl+d                       ###用ctrl+d 发起任务
[root@localhost  ~]#at  now+1min     ##延时1分钟
at>  rm  -fr  /mnt/*
at> ctrl+d


at  -l                      ##查看任务列表
at  -c  任务号                  ##查看任务内容


at -r   任务号                 ##取消任务执行


[root@localhost  ~]at   now+1min
at>  echo   hello
at>  ctrl+d


注意:
当任务有输出时,输出会以邮件的类型是发送给at任务的发起者
mail  -u  root   ###查看超级用户邮件
l                  ###查看第一份邮件
q                   ###退出
>  /var/spool/mail/root     ##清空邮件
cd  /mnt
touch  file{1..8}
监控     watch  -n  1  ls  /mnt   
vim  /etc/at.deny
vim   /etc/at.allow(白名单存在,黑名单就不检测)
###2.at任务的黑白名单###
/etc/at.deny     ##系统中默认存在,在此文件中出现的用户不能执行at


/etc/at.allow    ##系统中默认不存在,当文件出现时,普通用户不能执行,在此文件中出现的用户能执行at,并且黑名单失效。
#####3.系统定时任务##
1.crontab 时间表示方式
* * * * *        ##每分钟
*/2 * * * *       ##每2分钟
*/2 09-17 * * *     ###早7-晚5每两分钟
*/2 */2 * * *     #每隔2小时每2分钟
*/2 09-17 3,5 1 5     ##3月和5月每周周五及3月1日和5月1日早9晚5每2分钟
*/2 09-17 * * 5    ##每周周五早9晚五每2分钟
2.系统控制crontab的服务
crond.service     ##当程序开启时定时任务生效


3.crontab
crontab  -e  -u  ##执行此文件
crontab   -l  -u  ##显示此文件
crontab   -r  -u  ##删除此文件


4.文件方式设定定时任务
vim  /etc/cron.d/file
*  *  *  *  *   username   action
*  *  *  *  *  root  rm  -fr  /mnt/*


echo "* * * * *   root  rm  -fr  /mnt/*">/etc/cron.d/westos5.crontab  的黑白名单
/etc/cron.deny  ####系统中默认存在,在此文件中出现的用户不能执行crontab
/etc/cron.allow       ####系统中默认不存在,当文件出现时,普通用户不能执行,在此文件中出现的用户能执行crontab,并且黑名单失效。
##这两个黑名单中不会影响/etc/cron.d/目录中定时任务的发起及执行


###4.系统中临时文件的管理方式##
cd   /usr/lib/tmpfiles.d/
vim   westos.conf
d  /mnt/westos    777  root  root   8s
systemd-tmpfiles  --create  /usr/lib/tmpfiles.d/*

systemd-tmpfiles  --clean   /usr/lib/tmpfiles.d/*

猜你喜欢

转载自blog.csdn.net/weixin_42709236/article/details/81332157