一直没系统学过这个,今天是现上轿现扎耳朵眼儿,现学现卖
这个程序的目的是定时检查31个进程,始终保持31个中有10个在跑
- #!/bin/sh
- i=1
- running_count=0
- while [ $i -lt 32 ]
- do
- pcount=`ps -ef | grep "spider.php $i$" | wc -l`
- echo 'provid:' $i 'pcount:' $pcount
- if [ "$pcount" -gt 0 ]; then
- #echo "The process already run"
- running_count=`expr $running_count + $pcount`
- fi
- i=`expr $i + 1`
- done
- echo ""
- echo "running process count:" $running_count
- start_pcount=`expr 15 - $running_count`
- echo "will start process count:" $start_pcount
- echo ""
- if [ $start_pcount -ge 1 ]; then
- i=1
- n=0
- while [ $i -lt 32 ]
- do
- [ $n -ge $start_pcount ] && break
- pcount=`ps -ef | grep "spider.php $i$" | wc -l`
- if [ "$pcount" -eq 0 ]; then
- echo "start process $i"
- /usr/local/bin/php /data1/apache/htdocs/kaoshi.edu.sina.com.cn/_test/spider.php $i >> /usr/home/wanghui3/spider_$i.log 2>&1 &
- n=`expr $n + 1`
- fi
- i=`expr $i + 1`
- done
- fi
加在crontab中,定时执行:
*/2 * * * * cd /data1/apache/htdocs/xxxx.com.cn/_test && /home/wanghui3/spider_d >> /usr/home/wanghui3/spider_d.log 2>&1