PHP Script monitors nginx 502 error and automatically restarts PHP-FPM
This article mainly introduces PHP script monitoring Nginx 502 error and automatically restart PHP-FPM, this article directly give implementation code, and then put in cron execution, the need for friends can refer to the next
Recently the server occasionally appeared Nginx 502 bad Gateway, if in the computer next to good, if midnight or go out, how to do?
No problem, write a script to detect the status of the service, found an exception, automatic restart.
Automatic Restart Script:
The code is as follows:
$url = ' Http://blog.rebill.info ';
$cmd = '/usr/local/php/sbin/php-fpm restart ';
for ($i = 0; $i < 5; $i + +) {
$exec = "Curl connect-timeout 3-i $url 2>/dev/null";
$res = Shell_exec ($exec);
if (Stripos ($res, ' 502 Bad Gateway ')!== false) {
Shell_exec ($cmd);
Exit ();
}
}
The principle is to get the HTTP header with Curl and find the 502 status code to execute the command to restart the PHP-FPM.
URLs and CMD are changed to their own according to the actual situation. Then put it in the crontab one minute to execute it.
The code is as follows:
*/1 * * * */usr/bin/php/root/crontab/nginx502.php
Done!
http://www.bkjia.com/PHPjc/1000093.html www.bkjia.com true http://www.bkjia.com/PHPjc/1000093.html techarticle PHP Script monitors nginx 502 error and automatically restarts PHP-FPM this article mainly introduces the PHP script to monitor the Nginx 502 error and automatically restart PHP-FPM, this article gives the implementation code directly, and then put in cron ...