延时任务与定时任务

一、延时任务
at #此命令发起的延时任务都是一次性的

at 时间
at now+5minute 

at 00:31
>touch /mnt/file{1..3}
>ctrl+d
at -l     ##查看定时任务
at -c 4   ##查看指定编号为4的定时任务具体命令
at -r 4   ##删除编号为4的定时任务

[root@node1 mnt]# at 00:31
at> touch /mnt/file{1..3}
at> <EOT>                            ##ctrl+d表示发起动作
job 4 at Tue Apr 24 00:31:00 2018
[root@localhost ~]# at -l
4    Tue Apr 24 00:31:00 2018 a root
[root@localhost ~]# at -c 4

##编号为4的定时任务具体命令##

[root@localhost ~]# at -r 4

[root@localhost ~]# at -l

[root@localhost ~]#

#实验用监控命令 watch -n 1 ls /mnt/ -l


权力设定

/etc/at.deny   ##用户黑名单--默认存在,在此名单中出现的用户不能执行at命令
/etc/at.allow  ##用户白名单--默认不存在,但名单一旦出现,黑名单失效

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



二、定时任务---永久性的
方法一:
crontab -u username -e #指定用户建立定时任务
crontab -u username -l #指定用户查看定时任务
crontab -u username -r #指定用户删除定时任务
crontab -e #当前用户建立定时任务
crontab -l #当前用户查看定时任务
crontab -r #当前用户删除定时任务
分钟 小时 天 月 周  动作
*    *    *  *  *  #每分钟做一次
*   08-17 *  *  *  #每天8时到17时的每分钟做一次
*/2 08-17 *  *  *  #每天8时到17时的每两分钟做一次
*/2 08-17 1,15  *  *  #每月1日和15日8时到17时每两分钟做一次
*/2 08-17 1,15  3-5  3  #3到5月每月的1日和15日周三的8时到17时每两分钟做一次



[root@localhost ~]# crontab -u root -e  #建立定时任务
no crontab for root - using an empty one
crontab: installing new crontab
[root@localhost ~]# crontab -u root -l  #查看定时任务
* * * * * touch /mnt/file{1..3}     #在/mnt建立文件file{1..3},定时为每分钟
[root@localhost ~]# cat /var/spool/cron/root #此定时任务以文件形式存在
* * * * * touch /mnt/file{1..3}
[root@localhost ~]# crontab -u root -r #删除定时任务

[root@localhost ~]# crontab -u root -l                

no crontab for root

[root@localhost ~]# cat /var/spool/cron/root
cat: /var/spool/cron/root: No such file or directory
[root@localhost ~]#

方法二:
vim /etc/cron.d/filename
分钟 小时 天 月 周 用户 动作
* * * * * root touch /mnt/file{1..3}
#超级用户每分钟在/mnt下建立一次file{1..3}

步骤如下:


[root@localhost ~]# cd /etc/cron.d
[root@localhost cron.d]# ls
0hourly  raid-check  sysstat  unbound-anchor
[root@localhost cron.d]# vim tutu
[root@localhost cron.d]# cat tutu
* * * * * root touch /mnt/file{1..3}
[root@localhost cron.d]# crontab -l
no crontab for root
[root@localhost cron.d]#cd /var/spool/cron
[root@localhost cron.d]#ls
[root@localhost ~]# cd /etc/cron.d
[root@localhost cron.d]# ls
0hourly  raid-check  sysstat  tutu  unbound-anchor
[root@localhost cron.d]#

#[root@localhost cron.d]#cd /var/spool/cron  #用户级别
#[root@localhost ~]# cd /etc/cron.d  #系统级别

#黑名单--默认存在 /etc/cron.deny

用户黑名单,在此名单中出现的用户不能执行crontab命令


[root@localhost ~]# su - tutu
Last login: Tue Apr 24 02:03:11 EDT 2018 on pts/1
[tutu@localhost ~]$ crontab -e
no crontab for tutu - using an empty one
crontab: installing new crontab
[tutu@localhost ~]$ logout
[root@localhost ~]# vim /etc/cron.deny
[root@localhost ~]# cat /etc/cron.deny
tutu
[root@localhost ~]# su - tutu
Last login: Tue Apr 24 02:03:44 EDT 2018 on pts/1
[tutu@localhost ~]$ crontab -e
You (tutu) are not allowed to use this program (crontab)
See crontab(1) for more information
[tutu@localhost ~]$ logout
[root@localhost ~]#

#超级用户可以命令黑名单中的用户执行定时任务


[root@localhost ~]# cat /etc/cron.deny
tutu
[root@localhost ~]# su - tutu
Last login: Tue Apr 24 02:04:31 EDT 2018 on pts/1
[tutu@localhost ~]$ crontab -e
You (tutu) are not allowed to use this program (crontab)
See crontab(1) for more information
[tutu@localhost ~]$ logout
[root@localhost ~]# crontab -u tutu -e
crontab: installing new crontab
[root@localhost ~]#

#白名单--默认不存在
touch /etc/cron.allow
#用户白名单,名单默认不存在,但名单一旦出现,黑名单失效
#系统所有用户默认不能执行crontab,只有在名单中出现的用户可以使用

#白名单与黑名单文件只能出现其一,均出现时,仅读取白名单


[root@localhost ~]# cat /etc/cron.deny
[root@localhost ~]# touch /etc/cron.allow
[root@localhost ~]# cat /etc/cron.allow
[root@localhost ~]# su - tutu
Last login: Tue Apr 24 02:07:16 EDT 2018 on pts/1
[tutu@localhost ~]$ crontab -e
You (tutu) are not allowed to use this program (crontab)
See crontab(1) for more information
[tutu@localhost ~]$ logout
[root@localhost ~]# su - linux
Last login: Tue Apr 24 02:03:33 EDT 2018 on pts/1
[linux@localhost ~]$ crontab -e
You (linux) are not allowed to use this program (crontab)
See crontab(1) for more information
[linux@localhost ~]$ logout
[root@localhost ~]# vim /etc/cron.allow
[root@localhost ~]# cat /etc/cron.allow
tutu
[root@localhost ~]# su - tutu
Last login: Tue Apr 24 02:11:18 EDT 2018 on pts/1
[tutu@localhost ~]$ crontab -e
no crontab for tutu - using an empty one
crontab: installing new crontab
[tutu@localhost ~]$

三、系统临时文件的管理
/usr/lib/tmpfiles.d/*.conf #系统中临时文件的配置
tmp.conf #临时缓存文件

类型 名称    权限 所有人 所有组 存在时间
d  /mnt/tutu 1777 root   root   10s

[root@localhost cron.d]# cd /usr/lib/tmpfiles.d/
[root@localhost tmpfiles.d]# ls
abrt.conf                 lvm2.conf            spice-vdagentd.conf
cups.conf                 mdadm.conf           subscription-manager.conf
cups-lp.conf              pam.conf             systemd.conf
gvfsd-fuse-tmpfiles.conf  ppp.conf             systemd-nologin.conf
initscripts.conf          rpm.conf             tmp.conf
iscsi.conf                samba.conf           x11.conf
legacy.conf               saslauthd.conf
libselinux.conf           selinux-policy.conf
[root@localhost tmpfiles.d]# vim tutu.conf
[root@localhost tmpfiles.d]# cat tutu.conf
d /mnt/tutu 1777 root root 10s
[root@localhost tmpfiles.d]# systemd-tmpfiles --create /usr/lib/tmpfiles.d/*
[root@localhost tmpfiles.d]# touch /mnt/tutu/file
[root@localhost tmpfiles.d]# systemd-tmpfiles --clean /usr/lib/tmpfiles.d/*
[root@localhost tmpfiles.d]# touch /mnt/tutu/file
[root@localhost tmpfiles.d]# touch /mnt/tutu/file1
[root@localhost tmpfiles.d]# systemd-tmpfiles --clean /usr/lib/tmpfiles.d/*
[root@localhost tmpfiles.d]# systemd-tmpfiles --clean /usr/lib/tmpfiles.d/*
[root@localhost tmpfiles.d]#
#虚拟机用监控命令 watch -n 1 ls -R /mnt/ -l
ystemd-tmpfiles --create /usr/lib/tmpfiles.d/*  ##执行临时文件配置
systemd-tmpfiles --clean /usr/lib/tmpfiles.d/*  ##清理临时文件(存在时间在设置以上,存在时间不在设置以上的不做清理)

猜你喜欢

转载自blog.csdn.net/hh5820/article/details/80061097