詳解Linux定時任務Crontab的介紹與使用
一.cron介紹
linux內置的cron進程能幫我們實現(xiàn)這些需求,cron搭配shell腳本,非常復雜的指令也沒有問題。
1. var/spool/cron/
目錄下存放的是每個用戶包括root的crontab任務,每個任務以創(chuàng)建者的名字命名
2. _/etc/crontab
這個文件負責調度各種管理和維護任務。
3. /etc/cron.d/
這個目錄用來存放任何要執(zhí)行的crontab文件或腳本。
我們還可以把腳本放在/etc/cron.hourly、/etc/cron.daily、/etc/cron.weekly、/etc/cron.monthly目錄中,讓它每小時/天/星期、月執(zhí)行一次。
二.crontab的使用
我們常用的命令如下:
crontab [-u username] //省略用戶表表示操作當前用戶的crontab
1. -e (編輯工作表)
2. -l (列出工作表里的命令)
3. -r (刪除工作作)
我們用crontab -e進入當前用戶的工作表編輯,是常見的vim界面。每行是一條命令。
crontab的命令構成為 時間+動作,其時間有分、時、日、月、周五種,操作符有
- * 取值范圍內的所有數(shù)字
- / 每過多少個數(shù)字
- - 從X到Z
- ,散列數(shù)字
三.常見定時任務設置
實例1:每1分鐘執(zhí)行一次myCommand
<strong>* * * * * myCommand</strong>
實例2:每小時的第3和第15分鐘執(zhí)行
<strong>3,15 * * * * myCommand</strong>
實例3:在上午8點到11點的第3和第15分鐘執(zhí)行
<strong>3,15 8-11 * * * myCommand</strong>
實例4:每隔兩天的上午8點到11點的第3和第15分鐘執(zhí)行
<strong>3,15 8-11 */2 * * myCommand</strong>
實例5:每周一上午8點到11點的第3和第15分鐘執(zhí)行
<strong>3,15 8-11 * * 1 myCommand</strong>
實例6:每晚的21:30重啟smb
<strong>30 21 * * * /etc/init.d/smb restart</strong>
實例7:每月1、10、22日的4 : 45重啟smb
<strong>45 4 1,10,22 * * /etc/init.d/smb restart</strong>
實例8:每周六、周日的1 : 10重啟smb
<strong>10 1 * * 6,0 /etc/init.d/smb restart</strong>
實例9:每天18 : 00至23 : 00之間每隔30分鐘重啟smb
<strong>0,30 18-23 * * * /etc/init.d/smb restart</strong>
實例10:每星期六的晚上11 : 00 pm重啟smb
<strong>0 23 * * 6 /etc/init.d/smb restart</strong>
實例11:每一小時重啟smb
<strong>0 */1 * * * /etc/init.d/smb restart</strong>
實例12:晚上11點到早上7點之間,每隔一小時重啟smb
<strong>0 23-7/1 * * * /etc/init.d/smb restart</strong>
四.實例操作
1.文件實時寫入
1) 查看定時任務狀態(tài)
[root@localhost ~]#<strong>service crond status</strong> 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 <strong>(running)</strong> 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 ~]# <strong>service crond stop</strong> Redirecting to /bin/systemctl stop crond.service [root@localhost ~]# <strong>service crond start</strong> Redirecting to /bin/systemctl start crond.service
3) 編輯定時任務
[root@localhost ~]# crontab -e * * * * * echo `date '+\%Y-\%m-\%d \%H:\%M:\%S'` >> 123.txt #每分鐘執(zhí)行一次
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) 預制數(shù)據(jù):
[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) 檢查定時任務執(zhí)行,1分鐘左右
[root@localhost test20210920]# du -sh * 0 file1.log 0 file2.log 0 file3.log 0 file4.log 0 file5.log
4) 查看定時任務執(zhí)行情況:
[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格式錯誤,即沒有按照規(guī)則寫
查看原命令并修正:
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
以上就是詳解Linux定時任務Crontab的介紹與使用的詳細內容,更多關于Linux定時任務Crontab的資料請關注腳本之家其它相關文章!
相關文章
Linux實現(xiàn)文件定期本地備份/異地備份/刪除備份的腳本
數(shù)據(jù)備份的意義就在于,當受到網(wǎng)絡攻擊、入侵、電源故障或者操作失誤等事故的發(fā)生后,可以完整、快速、簡捷、可靠地恢復原有系統(tǒng)。本文為大家準備了文件定期本地備份/異地備份/刪除備份的腳本,希望對你們有所幫助2022-10-10shell腳本學習指南[六](Arnold Robbins & Nelson H
這篇文章主要介紹了shell腳本學習指南[六](Arnold Robbins & Nelson H.F. Beebe著),需要的朋友可以參考下2014-02-02