crontab定期執行命令詳解,以及例項
一,什麼是crond,crontab
linux下面定期分為二部分,一部分是後臺程式crond,一部分是crontab往crond輸入指令的介面。
為什麼要定期執行,因為有些事情我們是要定時執行的,這樣可以節省不少人力,物力。例 如:每個星期都要給註冊的使用者傳送郵件,如果能定時執行的話,就不要有個人去做了,現在sns比較流行,裡面有什麼好友新鮮事,你做的事情,不是立馬你的 好友就能看到,也許要過一小時,或者幾個小時才能看到,如果要人工去操作的話,就比較煩了。
二,crond啟動
crond不同的系統下面啟動不同,以archlinux為例
1,crond啟動
[root@BlackGhost zhangy]# crond
2,crond開機啟動
a,利用rc.conf
DAEMONS=(syslog-ng alsa hal net-profiles httpd !slim @mysqld !network !netfs crond)
b,利用rc.local
在檔案的最後加上sudo crond
三,crontab命令詳解
- [root@BlackGhost etc]# crontab -h
- crontab V3.2
-
crontab file
replace crontab from file //修改檔案存放位置 -
crontab -
replace crontab from stdin - crontab -u user specify user //指定使用者
- crontab -l [user] list crontab for user // 檢視命令列表
- crontab -e [user] edit crontab for user //編輯列表
- crontab -d [user] delete crontab for user //刪除列表
- crontab -c dir specify crontab directory //指定crontab 目錄
四,crontab命令詳解
1,crontab file 指定命令存放
archlinux下面預設crontab命令存放在是在/var/spool/cron/下面,以使用者名稱來命名檔案的
[root@BlackGhost etc]# ls /var/spool/cron
root zhangy
如果我想換個位置怎麼辦呢,看下面
- [root@BlackGhost etc]# cp /var/spool/cron/root /tmp/root
- [root@BlackGhost etc]# crontab /tmp/root //修改檔案存放位置
- [root@BlackGhost etc]# crontab -l
- #
- # DO NOT EDIT THIS FILE MANUALLY!! USE crontab -e INSTEAD.
- #
-
#
- 01 * * * * /usr/sbin/run-cron /etc/cron.hourly
- 02 00 * * * /usr/sbin/run-cron /etc/cron.daily
- 22 00 * * 0 /usr/sbin/run-cron /etc/cron.weekly
- 42 00 1 * * /usr/sbin/run-cron /etc/cron.monthly
- #30 00 * * * /sbin/shutdown -h now
- 00 18 * * * /home/zhangy/database_bak.sh
2,crontab -u,crontab -l,crontab -e,crontab -d
- [root@BlackGhost etc]# crontab -l -u zhangy //沒有任務
- [root@BlackGhost etc]# crontab -e -u zhangy //新增任務
- [root@BlackGhost etc]# crontab -l -u zhangy //檢視任務
- 00 18 * * * /home/zhangy/database_bak.sh
- [root@BlackGhost etc]# crontab -d -u zhangy //刪除任務使用者
- [root@BlackGhost etc]# crontab -l -u zhangy //使用者已刪除
- no crontab for zhangy
- [root@BlackGhost etc]# crontab -e -u zhangy //新增任務
- [root@BlackGhost etc]# crontab -l -u zhangy //檢視
- 00 18 * * * /home/zhangy/database_bak.sh
3,crontab書寫規則
第1列 第2列 3 4 5 6
第1列表示分鐘1~59 每分鐘用*或者 */1表示
第2列表示小時1~23(0表示0點)
第3列表示日期1~31
第4列表示月份1~12
第5列標識號星期0~6(0表示星期天)
第6列要執行的命令
crontab書寫規則的例子很多,以下從網上找的,偷懶一下,嘿嘿
30 21 * * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每晚的21:30重啟lighttpd 。
45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每月1、10、22日的4 : 45重啟lighttpd 。
10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每週六、週日的1 : 10重啟lighttpd 。
0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示在每天18 : 00至23 : 00之間每隔30分鐘重啟lighttpd 。
0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每星期六的11 : 00 pm重啟lighttpd 。
* */1 * * * /usr/local/etc/rc.d/lighttpd restart
每一小時重啟lighttpd
* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
晚上11點到早上7點之間,每隔一小時重啟lighttpd
0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
每月的4號與每週一到週三的11點重啟lighttpd
0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart
一月一號的4點重啟lighttpd
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/23490154/viewspace-1063804/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Crontab例項-命令詳解
- 在linux上定期執行命令、指令碼(cron,crontab,anacron)Linux指令碼
- crontab命令詳解
- Linux下的crontab定時執行任務命令詳解Linux
- 設定shell 定期 (crontab job) 執行 DB 中的物件物件
- 通過Linux crontab 定期執行Oracle Procedure 指令碼LinuxOracle指令碼
- Python程式和執行緒例項詳解Python執行緒
- Linux Crontab命令詳解Linux
- linux crontab 命令詳解Linux
- FTP命令詳解(含操作例項)FTP
- crontab異常:shell下可以執行命令,執行crontab卻報錯
- linux/unix crontab命令詳解Linux
- Crontab 定時任務命令詳解
- Debian的定時執行命令Crontab
- Linux crontab定時執行任務 命令格式與詳細例子Linux
- Centos 7下利用crontab定時執行任務詳解CentOS
- CentOS Linux使用crontab執行定時任務詳解CentOSLinux
- linux下find,xargs命令詳解和例項Linux
- curl例項詳解
- sudo 詳解+例項
- Linux Crontab 定時任務 命令詳解Linux
- crontab執行phpPHP
- crontab不執行
- 詳述Linux計劃任務的 at和crontab的命令 以及如何檢視crontab日誌Linux
- 元件例項 $el 詳解元件
- tcl/tk例項詳解——glob使用例解
- 掌握C#中的GUI多執行緒技巧:WinForms和WPF例項詳解C#GUI執行緒ORM
- java多執行緒例項Java執行緒
- 詳解樸素貝葉斯的來源,原理以及例項解析
- mybatis中的mapper介面檔案以及example類的例項函式以及詳解MyBatisAPP函式
- 30 個例項詳解 ,讓運維徹底搞清TOP 命令!運維
- 並查集例項詳解並查集
- Oracle 例項恢復詳解Oracle
- Activiti的流程例項【ProcessInstance】與執行例項【Execution】
- linux之crontab定時執行命令走過的坑坑Linux
- crontab執行shell指令碼指令碼
- crontab 每隔幾秒執行
- crontab的執行問題