Linux中使用crond工具創(chuàng)建定時(shí)任務(wù)的方法
- crond是一個(gè)linux下的定時(shí)執(zhí)行工具(相當(dāng)于windows下的scheduled task),可以在無(wú)需人工干預(yù)的情況下定時(shí)地運(yùn)行任務(wù)。crond工具提供crontab命令來(lái)設(shè)置定時(shí)任務(wù),屬于守護(hù)進(jìn)程,只能精確到分鐘,可以設(shè)定周期性執(zhí)行Linux命令或者Shell腳本,每分鐘crond都會(huì)檢查是否有定時(shí)任務(wù)需要執(zhí)行
- 本次實(shí)驗(yàn)系統(tǒng)為CentOS 7
操作步驟
(1) 檢查crond工具是否安裝
yum list installed | grep crontabs
若未安裝,則使用如下所示命令安裝
sudo yum install crontabs
(2) 檢查crond服務(wù)是否開啟由于是CentOS7所以使用 systemctl 命令,而非 service 命令
systemctl status crond.service
若未開啟,則使用如下所示命令開啟服務(wù)
sudo systemctl start crond.service
(3) 使用crond工具創(chuàng)建任務(wù)計(jì)劃crontab命令使用方法
Usage: crontab [options] file crontab [options] crontab -n [hostname] Options: -u <user> define user -e edit user's crontab -l list user's crontab -r delete user's crontab -i prompt before deleting -n <host> set host in cluster to run users' crontabs -c get host in cluster to run users' crontabs -s selinux context -x <mask> enable debugging # 注意 crontab -r 是刪除用戶的所有定時(shí)任務(wù)(慎用!)
可以通過 /etc/crontab 文件查看任務(wù)定義格式和設(shè)定任務(wù)執(zhí)行環(huán)境
以“每分鐘定時(shí)將日期寫入指定文件中”為例
方法1:使用crontab命令編輯當(dāng)前用戶定時(shí)任務(wù)(立即生效)**
crontab -e
在編輯器中插入如下指令(注意此時(shí)不要追加用戶,否則無(wú)法執(zhí)行,因?yàn)榇朔椒ㄊ侵苯釉O(shè)置當(dāng)前用戶的定時(shí)任務(wù))
*/1 * * * * date >> /home/TomAndersen/currentDate
檢查插入結(jié)果
[tomandersen@hadoop101 bin]$ crontab -l */1 * * * * date >> /home/TomAndersen/currentDate
方法2:編輯 /etc/crontab 文件,按照格式插入(生效較慢)**
*/1 * * * * tomandersen date >> /home/TomAndersen/currentDate
(4) 檢查是否設(shè)置成功
[tomandersen@hadoop101 bin]$ cat /home/TomAndersen/currentDate 2020年 02月 09日 星期日 18:12:01 CST 2020年 02月 09日 星期日 18:13:01 CST 2020年 02月 09日 星期日 18:14:01 CST 2020年 02月 09日 星期日 18:15:01 CST 2020年 02月 09日 星期日 18:16:02 CST 2020年 02月 09日 星期日 18:17:01 CST 2020年 02月 09日 星期日 18:18:01 CST 2020年 02月 09日 星期日 18:19:01 CST 2020年 02月 09日 星期日 18:20:01 CST
總結(jié)
以上所述是小編給大家介紹的Linux中使用crond工具創(chuàng)建定時(shí)任務(wù),希望對(duì)大家有所幫助!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。