詳解Linux定時(shí)任務(wù)Crontab的介紹與使用
一.cron介紹
linux內(nèi)置的cron進(jìn)程能幫我們實(shí)現(xiàn)這些需求,cron搭配shell腳本,非常復(fù)雜的指令也沒有問題。
1. var/spool/cron/
目錄下存放的是每個(gè)用戶包括root的crontab任務(wù),每個(gè)任務(wù)以創(chuàng)建者的名字命名
2. _/etc/crontab
這個(gè)文件負(fù)責(zé)調(diào)度各種管理和維護(hù)任務(wù)。
3. /etc/cron.d/
這個(gè)目錄用來存放任何要執(zhí)行的crontab文件或腳本。
我們還可以把腳本放在/etc/cron.hourly、/etc/cron.daily、/etc/cron.weekly、/etc/cron.monthly目錄中,讓它每小時(shí)/天/星期、月執(zhí)行一次。
二.crontab的使用
我們常用的命令如下:
crontab [-u username] //省略用戶表表示操作當(dāng)前用戶的crontab
1. -e (編輯工作表)
2. -l (列出工作表里的命令)
3. -r (刪除工作作)
我們用crontab -e進(jìn)入當(dāng)前用戶的工作表編輯,是常見的vim界面。每行是一條命令。
crontab的命令構(gòu)成為 時(shí)間+動(dòng)作,其時(shí)間有分、時(shí)、日、月、周五種,操作符有
- * 取值范圍內(nèi)的所有數(shù)字
- / 每過多少個(gè)數(shù)字
- - 從X到Z
- ,散列數(shù)字
三.常見定時(shí)任務(wù)設(shè)置
實(shí)例1:每1分鐘執(zhí)行一次myCommand
<strong>* * * * * myCommand</strong>
實(shí)例2:每小時(shí)的第3和第15分鐘執(zhí)行
<strong>3,15 * * * * myCommand</strong>
實(shí)例3:在上午8點(diǎn)到11點(diǎn)的第3和第15分鐘執(zhí)行
<strong>3,15 8-11 * * * myCommand</strong>
實(shí)例4:每隔兩天的上午8點(diǎn)到11點(diǎn)的第3和第15分鐘執(zhí)行
<strong>3,15 8-11 */2 * * myCommand</strong>
實(shí)例5:每周一上午8點(diǎn)到11點(diǎn)的第3和第15分鐘執(zhí)行
<strong>3,15 8-11 * * 1 myCommand</strong>
實(shí)例6:每晚的21:30重啟smb
<strong>30 21 * * * /etc/init.d/smb restart</strong>
實(shí)例7:每月1、10、22日的4 : 45重啟smb
<strong>45 4 1,10,22 * * /etc/init.d/smb restart</strong>
實(shí)例8:每周六、周日的1 : 10重啟smb
<strong>10 1 * * 6,0 /etc/init.d/smb restart</strong>
實(shí)例9:每天18 : 00至23 : 00之間每隔30分鐘重啟smb
<strong>0,30 18-23 * * * /etc/init.d/smb restart</strong>
實(shí)例10:每星期六的晚上11 : 00 pm重啟smb
<strong>0 23 * * 6 /etc/init.d/smb restart</strong>
實(shí)例11:每一小時(shí)重啟smb
<strong>0 */1 * * * /etc/init.d/smb restart</strong>
實(shí)例12:晚上11點(diǎn)到早上7點(diǎn)之間,每隔一小時(shí)重啟smb
<strong>0 23-7/1 * * * /etc/init.d/smb restart</strong>
四.實(shí)例操作
1.文件實(shí)時(shí)寫入
1) 查看定時(shí)任務(wù)狀態(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) 關(guān)閉/開啟定時(shí)任務(wù)
[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) 編輯定時(shí)任務(wù)
[root@localhost ~]# crontab -e * * * * * echo `date '+\%Y-\%m-\%d \%H:\%M:\%S'` >> 123.txt #每分鐘執(zhí)行一次
4) 查看已存在的定時(shí)任務(wù)
[root@localhost ~]# crontab -l * * * * * echo `date '+\%Y-\%m-\%d \%H:\%M:\%S'` >> 123.txt
5) 驗(yàn)證校驗(yàn)生成
[root@localhost ~]# cat 123.txt 2021-09-20 01:13:01 2021-09-20 01:14:01 2021-09-20 01:15:01
2. 定期清理對(duì)應(yīng)目錄下的文件
1) 預(yù)制數(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) 添加定時(shí)任務(wù)
[root@localhost ~]# crontab -e * * * * * find /root/test20210920 -type f -name '*.log' -exec cp /dev/null {} \;
3) 檢查定時(shí)任務(wù)執(zhí)行,1分鐘左右
[root@localhost test20210920]# du -sh * 0 file1.log 0 file2.log 0 file3.log 0 file4.log 0 file5.log
4) 查看定時(shí)任務(wù)執(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 {} \;)
五.常見錯(cuò)誤
1.errors in crontab file, can't install
解決方式:
因?yàn)槟愕腸rontab格式錯(cuò)誤,即沒有按照規(guī)則寫
查看原命令并修正:
echo `date +"%Y-%m-%d %H:%M"` >> 123.txt
修改后正常保存:
* * * * * echo `date +"%Y-%m-%d %H:%M"` >> 123.txt
2.接以上錯(cuò)誤:
郵件內(nèi)報(bào)錯(cuò):cat /var/spool/mail/root
解決方式:
crontab內(nèi)%需要轉(zhuǎn)義,修復(fù)定時(shí)任務(wù)正常
* * * * * echo `date '+\%Y-\%m-\%d \%H:\%M:\%S'` >> 123.txt
以上就是詳解Linux定時(shí)任務(wù)Crontab的介紹與使用的詳細(xì)內(nèi)容,更多關(guān)于Linux定時(shí)任務(wù)Crontab的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Linux全網(wǎng)最全面常用命令整理(附實(shí)例)
這篇文章主要介紹了Linux命令,是目前最全面的集合,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08shell耗時(shí)計(jì)算的實(shí)現(xiàn)
本文主要介紹了shell耗時(shí)計(jì)算的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01Linux實(shí)現(xiàn)文件定期本地備份/異地備份/刪除備份的腳本
數(shù)據(jù)備份的意義就在于,當(dāng)受到網(wǎng)絡(luò)攻擊、入侵、電源故障或者操作失誤等事故的發(fā)生后,可以完整、快速、簡(jiǎn)捷、可靠地恢復(fù)原有系統(tǒng)。本文為大家準(zhǔn)備了文件定期本地備份/異地備份/刪除備份的腳本,希望對(duì)你們有所幫助2022-10-10shell腳本學(xué)習(xí)指南[六](Arnold Robbins & Nelson H
這篇文章主要介紹了shell腳本學(xué)習(xí)指南[六](Arnold Robbins & Nelson H.F. Beebe著),需要的朋友可以參考下2014-02-02