php-cgi 重启,自动监测和重启 FastCGI 服务
昨天有個服務器出了點小問題,PHP FastCGI 進程無緣無故就死在那里了,造成 Nginx 不能和 FastCGI 通信,不能解析 PHP 頁面,只能看到 Nginx 的 默認 HTML 頁面。登錄到服務器檢查日志也沒有找到原因,重啟 FastCGI 后恢復正常。服務器上裝了 monit,如果服務關閉的話會自動重啟,但是這里 FastCGI 服務并沒有關閉,只不過由于某種原因不能和 Nginx 通信,所以 monit 誤認為 FastCGI 運行正常沒有執行重啟 FastCGI 的命令。
今天寫了一個 Python 腳本用來后臺監測 nginx 的日志,如果在日志里發現 111: Connection refused 或者 104: Connection reset by peer 等錯誤就 kill 掉所有 php-cgi 進程后重啟服務。程序有幾個地方需要說明:
1、如果不先 kill 直接運行 /etc/init.d/php-cgi restart 重啟服務有時候不管用,因為這時候 php-cgi 只是駐留在內存里的死進程而而已不能執行任何命令,所以需要 kill 全部 php-cgi 進程后再啟動;
2、讀 nginx 的 log 文件的時候要小心,log 文件通常很大,如果用 python 的普通讀文件函數會把整個文件讀出來后再處理,速度很慢,也會占用大量內存,所以最好用 tail 截取文件,我們只需要分析最后部分(也是最近)的記錄即可;
3、程序通過 /var/run/php_cgi.pid 來判斷 PHP FastCGI 是否正在運行,通過 /var/log/nginx/error.log 來判斷 Nginx/FastCGI 是否工作正常,每600秒檢查一次,如果工作不正常 sleep 2秒后重啟 php-cgi;
4、本程序可擴展到重啟其他服務,比如通過 /var/run/lighttpd.pid 來判斷 lighttpd 是否需要重啟;
5、程序中 daemonize 函數來自 Python Cookbook(O’Reilly ) 一書。
程序使用
拷貝下面代碼做一定修改以后保存為 checkphpcgi,增加文件可執行權限后用 root 運行程序:
# chmod +x checkphpcgi
# ./checkphpcgi
usage: ./checkphpcgi start|stop|restart
# ./checkphpcgi start
如果想要停止程序:
# ./checkphpcgi stop
程序代碼
#!/usr/bin/python
# Monitoring PHP/FastCGI processes, restart FastCGI if they:
# 1. crashed or
# 2. simply cannot communicate with Nginx
#
# written by http://www.vpsee.com
import sys, os, time
from signal import SIGTERM
pidfile = '/var/run/checkphpcgi.pid'
phpcgi_pidfile = '/var/run/php_cgi.pid'
phpcgi_command = '/etc/init.d/php_cgi restart'
nginx_logfile = '/var/log/nginx/error.log'
kill_phpcgi = 'killall -9 php-cgi'
def main():
argc = len(sys.argv)
if argc != 2:
print "usage: ./checkphpcgi start|stop|restart"
sys.exit(0)
action = sys.argv[1]
if action == 'start':
start()
elif action == 'stop':
stop()
elif action == 'restart':
stop()
start()
def start():
daemonize()
while True:
try:
f = file(phpcgi_pidfile, 'r')
pid = int(f.read().strip())
f.close()
except IOError:
pid = None
if not pid:
os.system(phpcgi_command)
elif pid > 0:
log = '/usr/bin/tail ' + nginx_logfile + ' > /tmp/nginx.log'
os.system(log)
f = file('/tmp/nginx.log')
text = f.read()
if text.find("111: Connection refused") or text.find("104: Connection reset by peer"):
os.system(kill_phpcgi)
time.sleep(2)
os.system(phpcgi_command)
f.close
os.remove('/tmp/nginx.log')
time.sleep(600)
def stop():
try:
f = file(pidfile, 'r')
pid = int(f.read().strip())
f.close()
except IOError:
pid = None
return
try:
os.kill(pid, SIGTERM)
os.remove(pidfile)
except OSError, err:
sys.stderr.write("not found checkphpcgi\n")
def daemonize(stdin = '/dev/null', stdout = 'dev/null', stderr='/dev/null'):
try:
pid = os.fork()
if pid > 0:
sys.exit(0)
except OSError, e:
sys.stderr.write("fork failed\n")
sys.exit(1)
os.chdir("/")
os.umask(0)
os.setsid()
try:
pid = os.fork()
if pid > 0:
sys.exit(0)
except OSError, e:
sys.stderr.write("fork failed\n")
sys.exit(1)
sys.stdout.flush()
sys.stderr.flush()
si = file(stdin, 'r')
so = file(stdout, 'a+')
se = file(stderr, 'a+', 0)
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
f = open(pidfile, "w")
f.write("%d" % os.getpid())
f.close()
if __name__=="__main__":
main()
總結
以上是生活随笔為你收集整理的php-cgi 重启,自动监测和重启 FastCGI 服务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《日长》第六句是什么
- 下一篇: 学富五车下一句是什么呢?