Linux 系统定时任务及延时任务
一,延迟任务
ctrl+d 发起任务
新建文件用来测试,发起监控
[[email protected] ~]# mkdir /test
[[email protected] ~]# touch /test/test{1..6}
[[email protected] ~]# watch -n 1 'ls -l /test/*' (图1)
(1)at 时间
在规定的时间完成任务
[[email protected] ~]# at 09:21 ##在09:21删除/mnt/*
at> rm -fr /mnt/*
(2)ctrl+d 发起任务 是上面写的任务开始执行
(3)at -r 任务编号 撤销任务
(4)at now+1min 在接下来一分钟执行此任务
(5)at -l 列出未执行的任务
(6)at -c 任务编号 查看任务内容
(7)用户的at权限
1》at权限的用户黑名单(当没有白名单时黑名单生效,当白名单存在时,黑名单失效,只有在白名单的用户拥有at权限)
黑名单:/etc/at.deny
[[email protected] ~]# cd /home
[[email protected] home]# ls
kiosk student
[[email protected] home]#
[[email protected] ~]# vim /etc/at.deny ##将student添加进黑名单
[[email protected] ~]#
切换用户到student,执行at命令,不可执行
[[email protected] home]# su - student
Last login: Sat Nov 3 20:36:06 CST 2018 on pts/0
[[email protected] ~]$ at mow+1min
You do not have permission to use at.
[[email protected] ~]$
切换用户到kiosk,执行at命令,可执行
[[email protected] ~]$ su - kiosk
Password:
Last login: Tue Nov 6 10:20:16 CST 2018 on :0
[[email protected] ~]$ at now+1min
at> touch file4
at> <EOT>
job 3 at Tue Nov 6 15:36:00 2018
[[email protected] ~]$ ls
Desktop Downloads Music Public Videos
Documents file4 Pictures Templates
[[email protected] ~]$
2》白名单
系统中不存在白名单,新建文件作为白名单,此时黑名单失效。只有在白名单的用户有at权限
将student添加至白名单
[[email protected] ~]# touch /etc/at.allow
[[email protected] ~]# vim /etc/at.allow
[[email protected] ~]#
分别用student和kiosk用户使用at
[[email protected] ~]# su - student
Last login: Tue Nov 6 15:43:28 CST 2018 on pts/2
[[email protected] ~]$ at now+1min
at> touch file1
at> <EOT>
job 5 at Tue Nov 6 15:47:00 2018
[[email protected] ~]$ su - kiosk
Password:
Last login: Tue Nov 6 15:35:04 CST 2018 on pts/2
[[email protected]undation68 ~]$ at now+1min
You do not have permission to use at.
[[email protected] ~]$
二、定时任务
1、linux 中的crond服务
crond服务通常被放在/etc/init.d/crond,这样就可以在系统重启后自动启动crond服务。
[[email protected] ~]# cd /etc/cron.
cron.d/ cron.daily/ cron.hourly/ cron.monthly/ cron.weekly/
linux中的用户使用contab命令来配置corn任务,crontab在/etc目录下面存在cron.d, cron.daily,cron.weekly,cron.monthly,cron.hourly五个目录和crontab文件
cron.d:系统自动定期需要做的人物,但是又不是按小时,按天,按星期,按月来执行的,那么就放在这个目录下面,如果是按小时,按天,按星期,按月来执行的话,则可以放到相应的目录下面
cron.daily是每天执行一次任务
cron.weekly是每周执行一次任务
cron.monthly是每月执行一次任务
cron.hourly是每小时执行一次任务
规定时间作规定的事
文件写入格式
1、用户级的crond
(1)开启crond服务
[[email protected] ~]# systemctl status crond.service
crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2018-11-06 10:20:06 CST; 5h 58min ago
Main PID: 1439 (crond)
CGroup: /system.slice/crond.service
└─1439 /usr/sbin/crond -n
Nov 06 10:20:06 foundation68.ilt.example.com systemd[1]: ...
Nov 06 10:20:06 foundation68.ilt.example.com systemd[1]: ...
Nov 06 10:20:07 foundation68.ilt.example.com crond[1439]: ...
Nov 06 10:20:09 foundation68.ilt.example.com crond[1439]: ...
Hint: Some lines were ellipsized, use -l to show in full.
[[email protected] ~]#
(2)查看写入格式
[[email protected] ~]# man 5 crontab
例如
*/2 10-13 * 2,5 3
每格2分钟 10点-13点 每天 2月和5月 周三
(3)设置定时任务
[[email protected] ~]# crontab -u root -e
no crontab for root - using an empty one
crontab: installing new crontab
[[email protected] ~]#
每天每时每分钟都删除mnt下的文件,此处路径必须时绝对路径
* * * * * rm -fr /mnt/*
(4)查看指定用户的定时任务
[[email protected] ~]# crontab -u root -l
* * * * * rm -fr /test/*
(5)普通用户定时设定
[[email protected] ~]# touch /home/student/westos{1..6}
[[email protected] ~]# crontab -u student -e
no crontab for student - using an empty one
crontab: installing new crontab
[[email protected] ~]#
* * * * * rm -fr /home/student/*
(6)也可以在/var/spool/cron/用户名 编辑定时任务
[[email protected] ~]# cat /var/spool/cron/root
[[email protected] ~]# vim /var/spool/cron/root
* * * * * rm -fr /mnt/*
(7)删除指定用户的定时任务
[[email protected] ~]# crontab -u student -r
[[email protected] ~]# crontab -u student -l
no crontab for student
[[email protected] ~]#
(8)用户的crontab权限黑白名单(与用户的at权限的黑白名单相似)
1》用户的crontab权限黑名单
将student用户添加进黑名单
[[email protected] ~]# vim /etc/cron.deny
[[email protected] ~]#
切换至student用户
[[email protected] ~]$ crontab -e
You (student) are not allowed to use this program (crontab)
See crontab(1) for more information
[[email protected] ~]$
切换至kiosk用户,有crontab权限
[[email protected] ~]$ su - kiosk
Password:
Last login: Tue Nov 6 15:49:39 CST 2018 on pts/2
[[email protected] ~]$ crontab -e
no crontab for kiosk - using an empty one
crontab: no changes made to crontab
[[email protected] ~]$
2》用户的crontab权限白名单
新建白名单文件,将student添加进白名单,此时黑名单失效
[[email protected] ~]# touch /etc/cron.allow
[[email protected] ~]# vim /etc/cron.allow
[[email protected] ~]#
分别切换至student用户和kiosk用户
[[email protected] ~]# su - student
Last login: Tue Nov 6 17:06:37 CST 2018 on pts/2
[[email protected] ~]$ crontab -e
crontab: no changes made to crontab
[[email protected] ~]$ su - kiosk
Password:
Last login: Tue Nov 6 17:07:26 CST 2018 on pts/2
[[email protected] ~]$ crontab -e
You (kiosk) are not allowed to use this program (crontab)
See crontab(1) for more information
[[email protected] ~]$
可以看出,由于student在白名单而kiosk没有在白名单,因此student用户有crontab权限,kiosk没有
2、系统级的crond
(1)系统级的crond文件都在/etc/cron.d
在/etc/cron.d目录下建立文件写入定时内容(内容需要注名用户)设置系统定时任务
[[email protected] ~]# cd /etc/cron.
cron.d/ cron.daily/ cron.hourly/ cron.monthly/ cron.weekly/
[[email protected] ~]# cd /etc/cron.d
[[email protected] cron.d]# ls
0hourly raid-check sysstat
[[email protected] cron.d]#
建立文件写入定时内容(内容需要注名用户)设置系统定时任务
[[email protected] cron.d]# vim westos
[[email protected] cron.d]#
写入内容
33 21 * * * root rm -fr /test/*
设置21:33删除/test/下的文件,在21:30时创建文件并关机,
(2)控制系统中的临时文件
临时文件所在目录/usr/lib/tmpfiles.d/
[[email protected] cron.d]# cd /usr/lib/tmpfiles.d/
[[email protected] tmpfiles.d]# ls
abrt.conf mdadm.conf setroubleshoot.conf
etc.conf pam.conf spice-vdagentd.conf
gvfsd-fuse-tmpfiles.conf ppp.conf subscription-manager.conf
httpd.conf python.conf systemd.conf
initscripts.conf radvd.conf systemd-nologin.conf
iscsi.conf rpcbind.conf tmp.conf
legacy.conf rpm.conf tuned.conf
libselinux.conf samba.conf var.conf
libstoragemgmt.conf sap.conf x11.conf
lvm2.conf selinux-policy.conf
文件tmp.conf写明了临时文件定时管理的格式
[[email protected] tmpfiles.d]# vim tmp.conf
[[email protected] tmpfiles.d]#
新建临时文件d的管理目录
[[email protected] tmpfiles.d]# vim test.conf
[[email protected] tmpfiles.d]#
d /test/ 777 root root 8s
目录/test/下的文件在系统中存在8s
[[email protected] test]# watch -n 1 ‘ls -l /test/’
此时监控无数据
在目录下创建文件
创建目录test.conf
[[email protected] test]# systemd-tmpfiles --create /usr/lib/tmpfiles.d/*
清理失效的文件
[[email protected] test]# systemd-tmpfiles --clean /usr/lib/tmpfiles.d/*