CentOS 下安裝crontab

 

***************************************************

0 0 * * * /usr/bin/rdate -s rdate.darkorb.net
0 0 * * * /bin/bash /data/nginx/cut_nginx_log.sh

# reboot server
0 5 * * 1 reboot

# update xiaoshuo
10 6,17,19,21,23 * * * /usr/local/webserver/php/bin/php /var/www/htdocs/crons/cron_tianyi_chapter.php > /dev/null 2>&1
40 12,18,22,2 * * * /usr/local/webserver/php/bin/php /var/www/htdocs/crons/cron_default_chapter.php > /dev/null 2>&1
1,31 *       * * * /data/python/caiji_asc.py > /dev/null 2>&1

5 * * * * /usr/local/php/bin/php /home/wwwroot/test_sinykk2.php > /dev/null 2>&1

***************************************************

 

注意 被執行的文件需要爲絕對路包含如 include("/home/wwwroot/testcron.php")

***************************************************************

 

 

 

安裝crontab:
[root@CentOS ~]# yum install vixie-cron
[root@CentOS ~]# yum install crontabs

說明:
vixie-cron軟件包是cron的主程序;
crontabs軟件包是用來安裝、卸裝、或列舉用來驅動 cron 守護進程的表格的程序。
//+++++++++++++++++++++++++++++++++++
cron 是linux的內置服務,但它不自動起來,可以用以下的方法啓動、關閉這個服務:
/sbin/service crond start //啓動服務
/sbin/service crond stop //關閉服務
/sbin/service crond restart //重啓服務
/sbin/service crond reload //重新載入配置

查看crontab服務狀態:service crond status

手動啓動crontab服務:service crond start

查看crontab服務是否已設置爲開機啓動,執行命令:ntsysv

加入開機自動啓動:
chkconfig --level 35 crond on


1,crontab命令

功能說明:設置計時器。

語  法:crontab [-u <用戶名稱>][配置文件] 或 crontab [-u <用戶名稱>][-elr]

補充說明:cron是一個常駐服務,它提供計時器的功能,讓用戶在特定的時間得以執行預設的指令或程序。只要用戶會編輯計時器的配置文件,就可以使用計時器的功能。其配置文件格式如下:
   Minute Hour Day Month DayOFWeek Command

參  數: 
 -e  編輯該用戶的計時器設置。 
 -l  列出該用戶的計時器設置。 
 -r  刪除該用戶的計時器設置。 
 -u<用戶名稱>  指定要設定計時器的用戶名稱。

2,crontab 格式

基本格式 :
*   *  *  *  *  command
分 時 日 月 周  命令

第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重啓apache。

45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每月1、10、22日的4 : 45重啓apache。

10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每週六、週日的1 : 10重啓apache。

0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示在每天18 : 00至23 : 00之間每隔30分鐘重啓apache。

0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每星期六的11 : 00 pm重啓apache。

* */1 * * * /usr/local/etc/rc.d/lighttpd restart
每一小時重啓apache

* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
晚上11點到早上7點之間,每隔一小時重啓apache

0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
每月的4號與每週一到週三的11點重啓apache

0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart
一月一號的4點重啓apache

*/30 * * * * /usr/sbin/ntpdate 210.72.145.44
每半小時同步一下時間

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章