crontab:能夠從定時重複工做中解脫出來linux
檢查cron服務數據庫
安裝cronapache
案例工具
[root@xuexi-001 ~]# crontab -e */1 * * * * date >> /tmp/log.txt # 每分鐘都打印當前時間到log.txt 文件中 [root@xuexi-001 ~]# tail -f /tmp/log.txt 2018年 10月 05日 星期五 00:00:01 CST 2018年 10月 05日 星期五 00:01:01 CST 2018年 10月 05日 星期五 00:02:01 CST # tail -f 顯示文件的最後幾行 ,tail -2 顯示文件的最後兩行
案例命令行
30 21 * * * service httpd restart
45 4 1,10,22 * * service httpd restart
45 4 1-10 * * service httpd restart
*/2 * * * * service httpd restart # 偶數分鐘 1-59/2 * * * * service httpd restart # 奇數分鐘
0 23-7/1 * * * service httpd restart
0,30 18-23 * * * service httpd restart 0-59/30 18-23 * * * service httpd restart
小結rest
crontab 選項:日誌
案例code
一、給root 用戶添加計劃任務:每隔兩分鐘(奇數分鐘)打印 「JISHU**********」crontab
[root@xuexi-001 ~]# crontab -e 1-59/2 * * * * echo "JISHU***********************"
二、添加普通用戶 crontester 而且添加計劃任務:每隔兩分鐘(偶數分鐘)打印「EVEN**************」it
[root@xuexi-001 ~]# useradd crontester [root@xuexi-001 ~]# crontab -e -u crontester [root@xuexi-001 ~]# crontab -l -u crontester 0-58/2 * * * * echo "EVEN ************"
查看日誌
[root@xuexi-001 ~]# tail -f /var/log/cron Oct 5 00:51:01 xuexi-001 CROND[1906]: (root) CMD (echo "JISHU***********************") Oct 5 00:52:01 xuexi-001 CROND[1911]: (crontester) CMD (echo "EVEN ************")
[root@xuexi-001 ~]# cd /var/log/ 您在 /var/spool/mail/root 中有新郵件 [root@xuexi-001 log]# ls cron* cron cron-20180611 cron-20180926 cron-20181001 [root@xuexi-001 log]# ls -l cron* -rw------- 1 root root 23564 10月 5 01:10 cron -rw-------. 1 root root 10917 6月 11 21:08 cron-20180611 -rw-------. 1 root root 11543 9月 26 20:13 cron-20180926 -rw------- 1 root root 4043 10月 1 13:22 cron-20181001 [root@xuexi-001 log]# tail -f cron Oct 5 01:04:01 xuexi-001 CROND[2001]: (crontester) CMD (echo "EVEN ************") Oct 5 01:04:49 xuexi-001 crontab[2005]: (root) LIST (root) Oct 5 01:04:49 xuexi-001 crontab[2005]: PAM pam_end: NULL pam handle passed Oct 5 01:05:01 xuexi-001 CROND[2007]: (root) CMD (echo "JISHU***********************") Oct 5 01:06:01 xuexi-001 CROND[2014]: (crontester) CMD (echo "EVEN ************") Oct 5 01:07:01 xuexi-001 CROND[2020]: (root) CMD (echo "JISHU***********************") Oct 5 01:08:01 xuexi-001 CROND[2026]: (crontester) CMD (echo "EVEN ************") Oct 5 01:09:01 xuexi-001 CROND[2032]: (root) CMD (echo "JISHU***********************")
案例
59 1 1-7 4 * test`date + \%w` -eq 0 && /root/a.sh
案例
錯誤示例: * 0,2,4,6,8,10,12,14,16,18,20,22 * * * date 正確示例: 0 */2 * * * date
說明
在使用分鐘的時候若是使用的分鐘時使用 * 那麼就是每兩個小時的每分鐘都會執行。正確的應該設置爲 0,還有就是在使用分鐘設置時,要注意分鐘的約束,好比1-10/2,先知足1-10之間的每兩分鐘也就是1,3,5,7,9的時候執行,在11分鐘的時候就不執行了。