**
Linux定时任务和延时任务
**
定时任务是永久的
延时任务是临时的
[root@client_ssh mnt]# at 16:13 ##设定任务执行时间
at> rm -fr /mnt/* ##任务动作
at> <EOT> ##用ctrl+D发起任务
在一分钟以后执行
[root@station mnt]# at now+1min
at> rm -fr /mnt/*
at> <EOT>
**
延时任务的黑白名单
**
黑名单
vim /etc/at.deny # 在黑名单中写入用户westos
westos用户不允许创建定时任务
白名单
系统中默认没有白名单,我们要按照黑名单的格式新建一个白名单vim /etc/at.allow
注意:白名单如果存在,则黑名单不被读取,如果黑白名单都写了westos用户,那么黑名单失效,westos是唯一可以创建延时任务的用户。
**
系统定时任务**
crontab -e | 创建定时任务 |
---|---|
crontab -l | 查看定时任务 |
crontab -r | 取消定时任务 |
查看定时任务是否开启
systemctl status crond.service
* * * * * | 表示每分钟 |
---|---|
*/2 * * * * | 表示每两分钟 |
*/2 09,17 * * * | 早9 晚五,每两分钟 |
*/2 09-17 * * * | 早9到晚五,每两分钟 |
*/2 09-17 1 * * | 每月1号的早上9点到下午5点,每两分钟 |
*/2 09-17 1 3 * | 每年3月1号的早上9点到下午5点,每两分钟 |
crontab -e -u student ##为student创建定时任务
crontab -l -u student ##查看student的定时任务
crontab -r -u student ##取消定时任务
定时任务的黑白名单
/etc/cron.deny ##系统中默认存在,在此文件中出现的用户不能执行crontab
/etc/cron.allow ##系统中默认不存在,当文件出现,普通用户不能执行crontab
##只有在名单中的用户可以,并且/etc/at.deny失效
##这两个名单都是不会影响/etc/cron.d/目录中定时任务的发起及执行
**
临时文件
**
系统中服务在正常运行时会产生临时文件
在系统中 /usr/lib/tmpfiles.d/ 标识服务的临时文件存放位置。
cd /usr/lib/tmpfiles.d/ ##临时文件存放位置
vim westos.conf ##后缀必须为.conf
d /mnt/westos 777 root root 10s
d文件类型 /mnt/westos表示要执行的文件 777表示对文件授予满权限 所有人所有组为root
systemd-tmpfiles –create /usr/lib/tmpfiles.d/* ##执行临时文件配置
systemd-tmpfiles –clean /usr/lib/tmpfiles.d/* ##清理临时文件