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系統臨時文件清理命令,結合上面的定時任務,就可以實現日常系統臨時文件的自動清理工作