日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

php-cgi 重启,自动监测和重启 FastCGI 服务

發布時間:2023/11/27 生活经验 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 服务的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。