linux 定时器
(1)实例
[root@iZ25tti3rxdZ log]# crontab -l
*/30 * * * * /home/whuang/software/auto_start_tomcat.sh
解说:
每隔30分钟执行,是从整点开始计时,1:30,2:00,2:30,3:00,3:30......
(2)定时器用在什么场合
比如有莫名其妙的进程启动,但是不知道是如何启动的.
那么可以增加定时器,每10分钟杀死进程
例如:
执行crontab -e
增加:
*/30 * * * * /home/whuang/software/auto_start_tomcat.sh
在脚本auto_start_tomcat.sh :
ps -ef |grep "/usr/bin/acpid" |grep -v grep |awk -F" " {'print $2'}|xargs -i kill -9 {}
ps -ef |grep "/usr/bin/bsd-port/agent" |grep -v grep |awk -F" " {'print $2'}|xargs -i kill -9 {}
ps -ef |grep "/usr/bin/.sshd" |grep -v grep|awk -F" " {'print $2'}|xargs -i kill -9 {}
ps -ef |grep "/root/.l" |grep -v grep|awk -F" " {'print $2'}|xargs -i kill -9 {}
ps -ef |grep "/mnt/linsx" |grep -v grep|awk -F" " {'print $2'}|xargs -i kill -9 {}
ps -ef |grep "getty" |grep -v grep|awk -F" " {'print $2'}|xargs -i kill -9 {}
(3)crontab的用法
57 14 15 * * /home/whuang/expect/a.sh |
每月的15号,14时57分执行脚本 /home/whuang/expect/a.sh
7 15 15 9 * /home/whuang/expect/a.sh |
9月的15号,15时7分执行脚本 /home/whuang/expect/a.sh
17 15 16 9 * /home/whuang/expect/a.sh |
9月的16号,15时17分执行脚本 /home/whuang/expect/a.sh
*/1 * * * * echo abc>>/home/whuang/expect/a.txt |
每分钟执行一次
0 */2 * * * echo “Have a break now.” >> /tmp/test.txt |
每两小时执行一次
*/1 * * * * /home/whuang/software/auto_start_tomcat.sh |
每隔一分钟就执行指定脚本