1.修改crontab文件添加或修改定時任務
直接編輯文件:nano /etc/crontab
或使用crontab命令:crontab -e
2.語法介紹:
里面已經有一些test示例,照著添加或修改就行,這里做點簡單介紹:
timeusercommand
分 ? ? ? ? ? ? ? ? 時 ? ? ? ? ? ? 天 ? ? ? ? ? 月 ? ? ? ? ? ? ? 星期 ? ? ? ? ? ? ? ? ? ?用戶 ? ? ? ? ? ? ?命令
minute ? ? ? ? hour ? ? ? ? day ? ? ? ? month ? ? ? ? dayofweek ? ? ? ? user ? ? ? ? ? ? ?command
A. 時間 time:
minute: 分鐘,從 0 到 59 之間的任何整數
hour: 小時,從 0 到 23 之間的任何整數
day: 日期,從 1 到 31 之間的任何整數(如果指定了月份,必須是該月份的有效日期)
month: 月份,從 1 到 12 之間的任何整數(或使用月份的英文簡寫如 jan、feb 等等)
dayofweek: 星期,從 0 到 7 之間的任何整數,這里的 0 或 7 代表星期日(或使用星期的英文簡寫如 sun、mon 等等)
command: 要執行的命令(command是linux終端可以直接執行的命令。)
這里一些符號可以表示以上特殊數值:
*表示所有有效值
-表示范圍, 比如1-4(從1到4)
,表示散數數值, 比如1,2,4,7...
/表示每隔,比如 */19 (每隔19 )
看一些簡單的例子,以下是我自己的一個crontab文件:
root@mtx:~#crontab -l
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow usercommand
#17 ** * *root ? ?cd / && run-parts --report /etc/cron.hourly#每小時的17分
#25 6* * *roottest -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )#每天6點25分
#47 6* * 7roottest -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )#每年7月份里每一天的6點47分
#52 61 * *roottest -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )#每個月1號的6點52分
*/4 * ? * * * ? root ? ?/home/Tool/httpslow/httpslow.sh &#每4分鐘
#
B. 命令 command
命令command直接是linux終端可以執行的命令或可執行腳本的絕對位置
3.使用方法:
crontab crontab_file (-u user)#如crontab /etc/crontab -u root (root用戶執行默認配置文件)
crontab -l#查看當前正在運行的crontab服務
crontab -r#刪除用戶crontab配置文件
crontab -i#刪除前給出提示
crontab -h#查看幫助
/etc/init.d/cron start/stop/restart/reload#啟動/停止/重啟/重新加載 crontab服務
簡單的臨時重復命令完全可以有其他方案代替:
while true;do clear && echo '您的ip地址是:' && wget -qo- ifconfig.me/ip;sleep 120;done
#每2分鐘刷新一次外網ip地址,并打印出來”
watch -n 5 netstat -nus#顯示網絡流量,每5秒刷新一次
。。。