respawn-spawn-fcgi.sh

前几天改用php-fpm后发现这玩意儿问题还没完,因为VPS的256MB内存很快就被几十个php-fpm进程占用光了。一些搜索结果的建议说放弃eAccelerator改用APC,真是链式问题啊。懒得折腾了,换回spawn-fcgi了。但是老是挂掉,怎么办呢?根据php创始人的理念

I'm not a real programmer. I throw together things until it works then I move on. The real programmers will say "yeah it works but you're leaking memory everywhere. Perhaps we should fix that." I'll just restart apache every 10 requests.

重启就重启吧。找了个shellscript,tail -f监控nginx的日志文件,发现[error]就重启。但是运行了几天发现有个问题,nginx的日志文件会定期(或者每文件大小?)压缩成.gz,而这个tail -f会一直监控老的file descriptor,所以文件更新了就不知道。在#gentoo-cn上学习到了logrotate这玩意儿。

vim /etc/logrotate.conf修改配置文件,在nginx的上下文里增加一行

postrotate
    /home/est/respawn-spawn-fcgi.sh &

然后写个/home/est/respawn-spawn-fcgi.sh脚本:

if [ -f /home/est/$0.pid ]
then
    kill `cat /home/est/$0.pid`
fi
echo $$ > /home/est/$0.pid

tail -fn0 "/var/log/nginx/blog.est.im.error_log" | \
while read line ; do
  echo "$line" | grep -e "\[error\].*blog_est_im-1.*111: Connection refused" > /dev/null
  if [ $? = 0 ]
  then
    date  --rfc-3339=seconds >> /home/est/respawn.txt
    /etc/init.d/spawn-fcgi.blog_est_im restart
  fi
done

希望这个问题就被我搞定了。

更新:

发现了tail --follow=name参数,就不用修改/etc/logrotate.conf了。直接写一个脚本

tail -n0 --follow=name "/var/log/nginx/blog.est.im.error_log" | \
while read line ; do
  echo "$line" | grep -e "\[error\].*blog_est_im-1.*111: Connection refused" > /dev/null
  if [ $? = 0 ]
  then
    date  --rfc-3339=seconds >> /home/est/respawn.txt
    /etc/init.d/spawn-fcgi.blog_est_im restart
  fi
done

然后 ./respawn-spawn-fcgi.sh & 就OK了。

Comments