1、延时任务:at命令(一次性延时任务)
使用格式:at TIME
输入需要执行的命令,按ctrl+d保存退出
参数:
at -l|atq ##查看当前任务列表
atrm |at -d +JOBSNU ##删除指定任务
at -c +JOBSNU ##查看任务内容
at now+1min ##一分钟后执行
at -f FILE ##执行文件中的内容(非脚本)
测试:
可以再打开一个tty,运行watch命令,监控任务的效果.
[root@rhel7 ~]# watch -n 1 ls /mnt
[root@rhel7 ~]# at 14:44 ##添加一个14:44分的任务
at> touch /mnt/file ##创建测试文件
at> <EOT> ##ctrl+d保存执行
job 6 at Wed Apr 25 14:44:00 2018
[root@rhel7 ~]# at -l ##查看当前任务
job 6 at Wed Apr 25 14:44:00 2018 a root ##编号6的任务信息
[root@rhel7 ~]# at now+1min ##添加一个1分钟后的任务
at> mv /mnt/file /mnt/jinx ##更名测试文件
at> <EOT> ##ctrl+d保存执行
job 7 at Wed Apr 25 14:47:00 2018
[root@rhel7 ~]# echo rm -f /mnt/* > /mnt/jinx ##给/mnt/jinx文件中输入一行命令
[root@rhel7 ~]# cat /mnt/jinx ##查看jinx文件内容
rm -f /mnt/jinx
[root@rhel7 ~]# at now+2min -f /mnt/jinx ##两分钟后执行jinx文件中的字符
job 8 at Wed Apr 25 14:53:00 2018
[root@rhel7 ~]# at -c 8 ##at-c查看任务8的内容
rm -f /mnt/jinx ##at命令可以直接运行文件中的字符
[root@rhel7 ~]# at -d 8 ##删除编号8的任务
[root@rhel7 ~]# at -l ##查看当前任务,删除成功
关于at命令中时间的格式,可以查看文档
[root@rhel7 ~]# cat /usr/share/doc/at-*/timespec ##*是at命令的版本号
at命令的执行权限
/etc/at.deny ##黑名单文件,所有名单中的用户都无法执行at命令
/etc/at.allow ##白名单文件,默认在系统中是不存在的,
创建此文件后,黑名单失效,并且只有白名单中存在的用户才有权限执行at命令
测试:
[root@rhel7 ~]# echo jinx > /etc/at.deny ##添加jinx用户至黑名单中
[root@rhel7 ~]# cat /etc/at.deny ##查看黑名单文件
jinx
[root@rhel7 ~]# su - jinx ##切换至jinx用户
Last login: Tue Apr 10 19:07:02 CST 2018 on :0
[jinx@rhel7 ~]$ at now+1min ##执行at命令
You do not have permission to use at. ##没有权限执行
[jinx@rhel7 ~]$ exit
logout
[root@rhel7 ~]# useradd atuser ##创建一个测试账户
[root@rhel7 ~]# echo jinx > /etc/at.allow ##添加用户jinx到白名单
[root@rhel7 ~]# cat /etc/at.allow ##查看白名单内容
jinx
[root@rhel7 ~]# su - atuser ##切换到atuser用户
[atuser@rhel7 ~]$ at now+1min ##执行at命令
You do not have permission to use at. ##无权限
[atuser@rhel7 ~]$ exit
logout
[root@rhel7 ~]# su - jinx ##切换到jinx用户
Last login: Wed Apr 25 15:24:08 CST 2018 on pts/1
[jinx@rhel7 ~]$ at now+1min ##执行at命令
at> ^C ##执行成功,黑名单中的内容失效
####讲卫生,好习惯####
[jinx@rhel7 ~]$ exit
logout
[root@rhel7 ~]# rm -f /etc/at.allow ##删除白名单文件
[root@rhel7 ~]# >/etc/at.deny ##清空黑名单文件
[root@rhel7 ~]# userdel -r atuser ##删除测试用户
2、定时任务:crontab
文档格式:
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
crontab任务有两种发起方法
第一种是通过crontab命令发起
crontab格式:
文档格式:
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * command to be execute
参数:
-u ##用户
-l ##显示
-r ##删除
-e ##编辑
试验:
[root@rhel7 mnt]# crontab -u jinx -e ##编辑jinx用户的定时任务
50 20 * * * touch file{1..5}
:wq ##保存退出
no crontab for jinx - using an empty one
crontab: installing new crontab ##创建了一个新任务
[root@rhel7 mnt]# crontab -u jinx -l ##查看当前jinx用户的任务
50 20 * * * touch file{1..5} ##每个20点50分执行“touch file{1..5}”命令
[root@rhel7 mnt]# ls -l /home/jinx/ | grep file* ##查看一下jinx用户定时任务创建的文件
-rw-r--r--. 1 jinx jinx 0 Apr 25 20:50 file1 ##20:50分自动执行任务创建了文件
-rw-r--r--. 1 jinx jinx 0 Apr 25 20:50 file2 ##因为是以jinx用户执行的的touch命令
-rw-r--r--. 1 jinx jinx 0 Apr 25 20:50 file3 ##文件名使用的是相对路径
-rw-r--r--. 1 jinx jinx 0 Apr 25 20:50 file4 ##所以文件创建到了jinx用户tty起始位置
-rw-r--r--. 1 jinx jinx 0 Apr 25 20:50 file5
[root@rhel7 mnt]# crontab -u jinx -l ##查看jinx用户的任务
50 20 * * * touch file{1..5}
##crontab命令执行时,是以vim打开或创建一个文件,这个文件的路径:/var/spool/cron/USERNAME
[root@rhel7 mnt]# cat /var/spool/cron/jinx ##查看文件内容
50 20 * * * touch file{1..5}
[root@rhel7 mnt]# ll /var/spool/cron/jinx ##查看文件属性
-rw-------. 1 root root 29 Apr 25 21:09 /var/spool/cron/jinx ##所属用户组都是root用户,其他用户---权限
[root@rhel7 mnt]# su - jinx ##切换到jinx用户
Last login: Wed Apr 25 15:38:20 CST 2018 on pts/1
[jinx@rhel7 ~]$ crontab -e ##尝试编辑任务
crontab: installing new crontab ##然而并没有什么卵用
[jinx@rhel7 ~]$ exit ##切换用户
logout
[root@rhel7 mnt]# ll /var/spool/cron/jinx ##查看任务文件属性
-rw-------. 1 jinx jinx 52 Apr 25 21:14 /var/spool/cron/jinx ##jinx用户执行crontab命令,又变过来了
[root@rhel7 mnt]# ll /usr/bin/crontab
-rwsr-xr-x. 1 root root 57576 Mar 30 2017 /usr/bin/crontab ##原来有特殊权限,所有用户执行时都以拥有者身份执行
[root@rhel7 mnt]# useradd test ##创建一个测试用户
[root@rhel7 mnt]# su - test ##试试普通用户是否可以更改其他用户任务
[test@rhel7 ~]$ crontab -u jinx -r ##删除jinx用户任务
must be privileged to use -u ##提示普通用户不能使用-u选项
[root@rhel7 mnt]# crontab -u jinx -r ##删除jinx用户的任务
[root@rhel7 mnt]# crontab -u jinx -l ##查看jinx用户的任务
no crontab for jinx ##任务删除成功
如果我们需要限制用户使用crontab命令,和at命令一样,也可以通过黑白名单来控制,白名单同样不存在,需要自行创建
黑名单文件:/etc/cron.deny
白名单文件:/etc/cron.allow
第二种编辑文件来实现定时任务
文档格式:
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
按照文件格式编写文件,放入/etc/cron.d/目录下
还有四个文件夹可以存放脚本,分别每天、小时、月、周自动执行
/etc/cron.daily ##脚本放入这个目录,每天执行一次
/etc/cron.hourly ##脚本放入这个目录,每小时执行一次
/etc/cron.monthly ##脚本放入这个目录,每月执行一次
/etc/cron.weekly/ ##脚本放入这个目录,每星期执行一次
时间格式
时间格式举例和意义是:
* * * * * ##每隔一分钟
* * */2 * * ##每隔两天
0 8-13/2 * * * ##早上八点到下午一点每隔两小时的整点
0 0 01,15 * 6 ##每个月的1号,15号以及星期六的零点
3、系统临时文件清理
系统在运行各种服务时,会产生大量临时文件,这就需要对系统产生的临时文件进行管理和清理
[root@desktop etc]# cd /usr/lib/tmpfiles.d/ ##对临时文件的管理,是通过编写"/usr/lib/tmpfiles/*.conf"文件来实现
[root@desktop tmpfiles.d]# pwd
/usr/lib/tmpfiles.d
[root@desktop tmpfiles.d]# vim test.conf ##创建一个conf文件
[root@desktop tmpfiles.d]# cat test.conf
d /mnt/test 1777 root root 10s ##d是文件类型,/mnt/westos是清理目录,1777是执行权限,root root是所有人和所属组,
##10s是文件存在时间
[root@desktop tmpfiles.d]# systemd-tmpfiles --create /usr/lib/tmpfiles.d/test.conf ##创建
[root@desktop tmpfiles.d]# cd /mnt/test
[root@desktop test]# ll
total 0
[root@desktop test]# touch file1
[root@desktop test]# touch file2
[root@desktop test]# touch file3
[root@desktop test]# systemd-tmpfiles --clean /usr/lib/tmpfiles.d/test.conf ##清理
[root@desktop test]# ll
total 0
-rw-r--r-- 1 root root 0 Apr 25 22:23 file3 ##file3文件生成时间不足10s,没有被删除
systemd-tmpfiles系统临时文件清理命令,结合上面的定时任务,就可以实现日常系统临时文件的自动清理工作