Linux Crontab 定時任務


一.cron介紹
linux內置的cron進程能幫我們實現這些需求,cron搭配shell腳本,非常復雜的指令也沒有問題。
1. var/spool/cron/

目錄下存放的是每個用戶包括root的crontab任務,每個任務以創建者的名字命名
2. _/etc/crontab

這個文件負責調度各種管理和維護任務。
3. /etc/cron.d/

這個目錄用來存放任何要執行的crontab文件或腳本。
我們還可以把腳本放在/etc/cron.hourly、/etc/cron.daily、/etc/cron.weekly、/etc/cron.monthly目錄中,讓它每小時/天/星期、月執行一次。

 

二.crontab的使用
我們常用的命令如下:
crontab [-u username]    //省略用戶表表示操作當前用戶的crontab
1. -e (編輯工作表)
2. -l (列出工作表里的命令)
3. -r (刪除工作作)
我們用crontab -e進入當前用戶的工作表編輯,是常見的vim界面。每行是一條命令。
crontab的命令構成為 時間+動作,其時間有分、時、日、月、周五種,操作符有
* 取值范圍內的所有數字
/ 每過多少個數字
- 從X到Z
,散列數字

 

三.常見定時任務設置
實例1:每1分鍾執行一次myCommand

* * * * * myCommand

實例2:每小時的第3和第15分鍾執行

3,15 * * * * myCommand

實例3:在上午8點到11點的第3和第15分鍾執行

3,15 8-11 * * * myCommand

實例4:每隔兩天的上午8點到11點的第3和第15分鍾執行

3,15 8-11 */2 * * myCommand

實例5:每周一上午8點到11點的第3和第15分鍾執行

3,15 8-11 * * 1 myCommand

實例6:每晚的21:30重啟smb

30 21 * * * /etc/init.d/smb restart

實例7:每月1、10、22日的4 : 45重啟smb

45 4 1,10,22 * * /etc/init.d/smb restart

實例8:每周六、周日的1 : 10重啟smb

10 1 * * 6,0 /etc/init.d/smb restart

實例9:每天18 : 00至23 : 00之間每隔30分鍾重啟smb

0,30 18-23 * * * /etc/init.d/smb restart

實例10:每星期六的晚上11 : 00 pm重啟smb

0 23 * * 6 /etc/init.d/smb restart

實例11:每一小時重啟smb

0 */1 * * * /etc/init.d/smb restart

實例12:晚上11點到早上7點之間,每隔一小時重啟smb

0 23-7/1 * * * /etc/init.d/smb restart

 

四.實例操作

1.文件實時寫入:

1) 查看定時任務狀態

[root@localhost ~]#service crond status Redirecting to /bin/systemctl status crond.service ● crond.service - Command Scheduler Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2021-09-20 01:22:18 CST; 24s ago Main PID: 17516 (crond) CGroup: /system.slice/crond.service └─17516 /usr/sbin/crond -n

2) 關閉/開啟定時任務

[root@localhost ~]# service crond stop Redirecting to /bin/systemctl stop crond.service [root@localhost ~]# service crond start Redirecting to /bin/systemctl start crond.service

3) 編輯定時任務

[root@localhost ~]# crontab -e * * * * * echo `date '+\%Y-\%m-\%d \%H:\%M:\%S'` >> 123.txt #每分鍾執行一次

4) 查看已存在的定時任務

[root@localhost ~]# crontab -l * * * * * echo `date '+\%Y-\%m-\%d \%H:\%M:\%S'` >> 123.txt

5) 驗證校驗生成

[root@localhost ~]# cat 123.txt 2021-09-20 01:13:01
2021-09-20 01:14:01
2021-09-20 01:15:01

2. 定期清理對應目錄下的文件

1) 預制數據:

[root@localhost test20210920]# echo abcdefg | tee -a file{1..5}.log abcdefg [root@localhost test20210920]# du -sh *
4.0K file1.log 4.0K file2.log 4.0K file3.log 4.0K file4.log 4.0K    file5.log

2) 添加定時任務

[root@localhost ~]# crontab -e * * * * * find  /root/test20210920  -type f -name '*.log' -exec cp /dev/null {} \;

3) 檢查定時任務執行,1分鍾左右

[root@localhost test20210920]# du -sh *
0 file1.log 0 file2.log 0 file3.log 0 file4.log 0    file5.log

4) 查看定時任務執行情況:

[root@localhost test20210920]# tail -5 /var/log/cron Sep 20 01:56:33 localhost crontab[17797]: (root) END EDIT (root) Sep 20 01:56:51 localhost crontab[17802]: (root) BEGIN EDIT (root) Sep 20 01:56:54 localhost crontab[17802]: (root) END EDIT (root) Sep 20 01:57:01 localhost CROND[17809]: (root) CMD (find  /root/test20210920  -type f -name '*.log' -exec cp /dev/null {} \;) Sep 20 01:58:01 localhost CROND[17819]: (root) CMD (find  /root/test20210920  -type f -name '*.log' -exec cp /dev/null {} \;)

 

五.常見錯誤

1.errors in crontab file, can't install

 解決方式:

因為你的crontab格式錯誤,即沒有按照規則寫

查看原命令並修正:

echo `date +"%Y-%m-%d %H:%M"` >> 123.txt

修改后正常保存:

* * * * * echo `date +"%Y-%m-%d %H:%M"` >> 123.txt

 2.接以上錯誤:

郵件內報錯:cat /var/spool/mail/root

解決方式: 

crontab內%需要轉義,修復定時任務正常

* * * * * echo `date '+\%Y-\%m-\%d \%H:\%M:\%S'` >> 123.txt

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM