-
Crontab定時任務(wù)
crond進程
為linux下crontab的進程,系統(tǒng)安裝后會默認安裝啟動此進程,此進程每分鐘會進行檢查是否有要執(zhí)行的任務(wù)
image.png
系統(tǒng)任務(wù)調(diào)度配置文件
路徑 /etc/crontab
- SHELL變量指定使用的shell
- PATH變量指定系統(tǒng)執(zhí)行命令的路徑
- MAILTO變量指定crond任務(wù)執(zhí)行信息發(fā)送給root,若為空,則表示不發(fā)送執(zhí)行信息給用戶
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# 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
image.png
用戶任務(wù)調(diào)度
- 用戶定期要執(zhí)行的工作,比如用戶數(shù)據(jù)備份,定時郵件提醒等;
- 用戶可以使用 crontab工具來定制自己的計劃任務(wù);
- 所有用戶定義的crontab 文件都被保存在 /var/spool/cron目錄中,其文件名與用戶名一致
image.png
使用者權(quán)限文件
- /etc/cron.deny文件中用戶不允許使用crontab命令
- /etc/cron.allow文件中用戶允許使用crontab命令
crontab格式說明
- 任務(wù)定義:minute hour day month week command
- 特殊字符在任務(wù)定義中的含義
| 符號 | 含義 | 示例 |
|---|---|---|
| * | 表示任意 | *****表示每分鐘 |
| , | 分割范圍 | 1,3,4,5 |
| - | 范圍值 | 2-6 |
| / | 間隔頻率 | 0-23/2每兩小時執(zhí)行一次 在minutes字段*/10表示10分鐘執(zhí)行一次 |
image.png
服務(wù)操作說明
systemctl start/stop/restart/status/reload crond
crontab命令
crontab [-u user] file
crontab [-u user] [ -e/-l/-r ]
參數(shù)詳情:
- -u user:用來設(shè)定某個用戶的crontab服務(wù),此參數(shù)一般有root用戶來運行
- file:file是命令文件的名字,表示將file做為crontab的任務(wù)列表文件并載入crontab,如果在命令行中沒有指定這個文件,crontab命令將接受標準輸入上鍵入的命令,并將它們載入crontab
- -e:編輯某個用戶的crontab文件內(nèi)容,如果不指定用戶,則表示編輯當前用戶的crontab文件
- -l:顯示某個用戶的crontab文件內(nèi)容,如果不指定用戶,則表示顯示當前用戶的crontab文件內(nèi)容
- -r:從/var/spool/cron目錄中刪除某個用戶的crontab文件,如果不指定用戶,則默認刪除當前用戶的crontab文件
- -i:在刪除用戶的crontab文件時給確認提示
image.png
創(chuàng)建crontab文件
- 新建保存crontab命令的文件
image.png
- 將文件內(nèi)容加入cron進程
crontab delete_log.cron
image.png
- 查看列出當前crontab文件
crontab -l
image.png
- 備份當前的crontab文件
image.png
- 編輯crontab文件
crontab -e
編輯方式同vi,wq保存退出
image.png
image.png
- 刪除當前crontab文件
image.png
- 恢復誤刪的crontab文件
方式一:將備份文件直接寫入crontab文件存儲地 /var/spool/cron/root(root用戶)
方式二:crontab命令將文件加入cron進程 crontab $HOME/cron_bak/mycorn
image.png
- 動態(tài)打印任務(wù)執(zhí)行結(jié)果
tail -f /var/log/cron
image.png
- 查看郵件
more /var/spool/mail/root
image.png
定時任務(wù)格式說明
任務(wù)格式:minute hour day month week command
| 任務(wù)格式 | 說明 |
|---|---|
| * * * * * command | 每分鐘執(zhí)行一次命令 |
| 3,15 * * * * command | 每小時的3分15分執(zhí)行任務(wù) |
| 3,15 8-11 * * * command | 每天上午8點到11點每小時的3分15分執(zhí)行任務(wù) |
| 3,15 8-11 */2 * * command | 每隔兩天的上午8點到11點每小時的3分15分執(zhí)行任務(wù) |
| 3,15 8-11 * * 1 command | 每周一的上午8點到11點每小時的3分15分執(zhí)行任務(wù) |
| 30 21 * * * command | 每天的21點30分執(zhí)行命令 |
| 45 4 1,10,22 * * command | 每月的1日10日22日4點45分執(zhí)行命令 |
| 10 1 * * 6,0 command | 每周六周日1點10分執(zhí)行命令 |
| 0,30 18-23 * * * command | 每天18點到23點每半個小時執(zhí)行命令 |
| 0 23 * * 6 command | 每周六的23點執(zhí)行命令 |
| * */1 * * * command | 每小時執(zhí)行命令 |
| * 23-7/1 * * * command | 每天23點到7點的每小時執(zhí)行命令 |
| 0 11 4 * mon-wed command | 每月4號和每周一到周三的11點執(zhí)行命令 |
| 0 4 1 jan * command | 每年一月一號的4點執(zhí)行命令 |
注意事項
- 腳本中(shell)涉及到其他環(huán)境變量時,通過source命令引入
- 腳本中(shell)涉及到路徑時,必須使用全局路徑,否則可能導致腳本運行無結(jié)果
- 任務(wù)調(diào)度分為系統(tǒng)任務(wù)調(diào)度與用戶任務(wù)調(diào)度,注意重啟系統(tǒng)的任務(wù),必須放入 /etc/crontab文件中
- 創(chuàng)建新的cron任務(wù),一般需要等2分鐘才會執(zhí)行,重啟cron可立即執(zhí)行
- 在crontab中%代表換行的意思,若要用其本意,則需要轉(zhuǎn)義%,如date ‘+%Y%m%d’
- 每條任務(wù)調(diào)度執(zhí)行完畢,系統(tǒng)都會將任務(wù)輸出信息通過電子郵件的形式發(fā)送給當前系統(tǒng)用戶,為防止日志文件過大,常使用定時任務(wù)對日志文件進行重定向
-
Shell腳本
- 創(chuàng)建文件的小腳本
#! /bin/bash
#通過輸入的值創(chuàng)建文件
read -p "Please input a num : " a
for i in `seq 1 $a`;do
#保留兩位數(shù)
j=$(printf "%.2d" $i)
touch test_log.$j
done
image.png
- 刪除后綴大于10的文件
#! /bin/bash
#刪除后綴大于10的文件
for i in $(ls /home/Lv0/0327/);do
#從文件名中以.分割,取第2區(qū)數(shù)字,并在數(shù)字中去除開頭的0(防止shell識別不了其為數(shù)字)
j=$(echo $i|awk -F '.' '{print $2}'|sed 's/^0*//')
if (($j>=10));then
rm -rf /home/Lv0/0327/$i
fi
done
image.png
-
cron+shell定時執(zhí)行
image.png

















