Linux基础——系统定时任务

########一、at 延时命令########

watch ls /mnt/ -l                      ##监控命令

有两种方式执行延时命令:

1、at 时间                

2、at now+5min

  at 16:00                                 ##在16点时  

>touch /mnt/file{1..10}            ##在16点建立/mnt下file{1..10}文件

>ctrl+d                                   ##执行任务

at -l                                         ##查看任务

at -c 3                                     ##查看详细任务

at -r 3                                     ##删除任务


at now+2min                           ##从系统时间现在开始过2分钟

>touch /mnt/file{1..10}             ##系统时间2分钟后建立/mnt下file{1..10}文件

>ctrl+d                                    ##执行任务

例:at 7:20 时删除/mnt下的所有文件:


at now+2min 新建/mnt/file{1..5}:


at 黑白名单的建立

vim   /etc/at.deny               ##编辑at黑名单

touch  /etc/at.allow            ##创建at白名单

vim   /etc/at.allow              ##编辑at白名单

例:黑名单中填写westos:westos用户无法使用at命令,黑名单外的所有用户都可以使用at命令



创建白名单。白名单中填写westos:除root用户外只有westos用户可以使用at命令,其他用户都不可以使用at命令




########二、crontab 定时命令########

crontab 编写格式:

 *       *         *      *       *

   小时               

crontab -u root -e                      ##编写定时任务

例:43 1 * * *  touch /mnt/file{1..5}

37 1 * * * rm -fr /mnt/*

crontab -u root -l                      ##查看定时任务

crontab所在文件:cat /var/spool/cron/root

crontab -u root -r                      ##删除定时任务

例:编写定时任务1:37分删除/mnt/下所有文件,查看定时任务:


/mnt/下所有文件在1:37时全删除了:


crontab -u root -r  删除了 crontab -u root -e 创建的定时任务:


crontab 黑白名单的建立

vim /etc/cron.deny              ##编辑crontab黑名单

touch /etc/cron.allow          ##建立crontab白名单

vim /etc/cron.allow              ##编辑crontab白名单

例:crontab黑名单中的westos用户无法使用crontab命令,黑名单外的所有用户都可以使用crontab命令


创建crontab白名单。白名单中填写westos:除root用户外只有westos用户可以使用crontab命令,其他用户都不可以使用crontab命令



系统级的crontab                                                 ##只能超级用户编辑

  /etc/cron.d/                                                     ##用户自定义执行模式

  /etc/cron.daily/                                                ##每天执行1次

  /etc/cron.hourly/                                              ##每小时执行1次

  /etc/cron.monthly/                                           ##每月执行1次

  /etc/cron.weekly/                                             ##每周执行1次

  建立自定义的crontab时,需要指定用户


例:watch ls /mnt/ -l                                          ##打开另一个shell监控

cd /etc/cron.                       

cd /etc/cron.d                                                    ##自定义定时任务

vim westos:                                                        ##编辑westos文件

 * * * * * root touch /mnt/file{1..10}                     ##在下一分下一秒建立/mnt/file{1..10}

cat westos

注意:系统级的定时任务不会在用户级的文件中显示

crontab -l无法查看

例:在/etc/cron.d/中建立westos文件,创建下一分下一秒建立/mnt/file{1..10}文件的命令:



########三、临时文件管理########

第一步:cd - /usr/lib/tmpfiles.d

第二步:vim westos.conf:                                    ##编辑临时文件westos

d /mnt/westos 1777 root root 5s                        ##对/mnt/westos和westos目录下的临时文件权限为1777,时间为5s

第三步:systemd-tmpfiles --create /usr/lib/tmpfiles.d/*    ##建立临时文件

第四步:touch /mnt/westos/file                            

touch /mnt/westos/file1

第五步:systemd-tmpfiles --clean /usr/lib/tmpfiles.d/*    ##删除临时文件:删除/mnt/westos/下的文件,建立时间小于5s的无法删除,大于5s的才可删除

例:编辑临时文件westos,建立临时文件:


建立touch /mnt/westos/file,touch /mnt/westos/file1:


删除临时文件:删除/mnt/westos/下的文件,建立时间小于5s的无法删除,大于5s的才可删除:






猜你喜欢

转载自blog.csdn.net/weixin_41927237/article/details/80106808