linux——延时任务与定时任务
目錄
一、延時任務
二、定時任務---永久性的
三、系統臨時文件的管理
一、延時任務
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/*? ##清理臨時文件(存在時間在設置以上,存在時間不在設置以上的不做清理)
總結
以上是生活随笔為你收集整理的linux——延时任务与定时任务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux——关于ip、静态网络、动态网
- 下一篇: linux——DHCP的配置问题