日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux——延时任务与定时任务

發布時間:2025/3/19 linux 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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——延时任务与定时任务的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。