linu - 系统延时与定时任务

系统延时及定时任务


1.系统延时任务


at命令发起的延时任务都时一次性

at + time
[[email protected] ~]# at 11:11
at> rm -fr /mnt/*            ##延迟动作
at> <EOT>                       ##ctrl+d表示发起动作
job 6 at Tue Aug  1 11:11:00 2017


at -l | atq                             #查看当前任务
atrm  | at -d                        #取消指定任务
at -c                                     #查看任务内容
at now+1min                     #延迟一分钟
at -f file                               #延迟执行文件中的内容
at -m                                    #延迟命令没有输出时仍然发送邮件给执行者
at -M                                    #延迟命令有输出时但不发送邮件给执行者


用监控命令查看  watch -n 1 ls -l /mnt/

linu - 系统延时与定时任务

linu - 系统延时与定时任务

查看邮件  mail

linu - 系统延时与定时任务


#### at 命令的执行权力设定 ####


/etc/at.deny               ##用户黑名单,在此名单中出现的用户不能执行at命令


/etc/at.allow               ##用户白名单,名单默认不存在,但名单一旦出现,黑名单失效


                                    ##系统所有用户默认不能执行at,只有在名单中出现的用户可以使用at命令



2.定时任务


#定时任务是永久的

#定时任务程序crond,在系统中每分钟做任务扫描

#发起方式一#
crontab -u username -e
crontab -u username -r
crontab -u username -l

分钟     小时    天         月        周          事件
*              *       */2      *          *                                             #每隔两天
12            12      *         *          *          rm -fr /mnt/*  
          #每天的12:12删除mnt里的内容

*               *         1,2       *         *          rm -fr /mnt/*              #每月的1号和2号删除mnt里的内容

*               *         1-3       *         *         rm-fr /mnt/*             #每月的1号到3 号删除mnt里的内容
*/2            6-18      *        *       1-5       rm -fr /mnt/*            #周一但周五早上6点到下午6点每隔两分钟删除mnt里的内容


  查看状态  systemctl  status crond.service

linu - 系统延时与定时任务
crontab -e

crontab -l   列出当前用户的cron任务

crontab -r   删除当前用户的cron任务

linu - 系统延时与定时任务

linu - 系统延时与定时任务

crontab 命令的执行权力设定


/etc/cron.deny  ##用户黑名单,在此名单中出现的用户不能执行crontab命令


/etc/cron.allow  ##用户白名单,名单默认不存在,但名单一旦出现,黑名单失效


   ##系统所有用户默认不能执行crontab,只有在名单中出现的用户可以使用



linu - 系统延时与定时任务


linu - 系统延时与定时任务


linu - 系统延时与定时任务

不同用户执行各自任务

vim  /var/spool/cron/root

* * * * * touch /tmp/file{1,2}

vim /var/spool/cron/student

* * * * * touch /tmp/file{3,4}


linu - 系统延时与定时任务

linu - 系统延时与定时任务


3.系统临时文件的管理


系统中服务在正常运行时会产生临时文件
/usr/lib/tmpfiles.d/*.conf                  ##系统中临时文件的配置


文件类型 文件名称 文件权限 文件所有人 文件所有组 文件存在时间


d  /mnt/westos 777  root  root  10s


systemd-tmpfiles --create /usr/lib/tmpfiles.d/* ##执行临时文件配置


systemd-tmpfiles --clean /usr/lib/tmpfiles.d/* ##清里临时文件


linu - 系统延时与定时任务

linu - 系统延时与定时任务

linu - 系统延时与定时任务


 本文转自 huanzi2017 51CTO博客,原文链接:http://blog.51cto.com/13362895/1978214