Nginx 502 Bad Gateway automatic restart script

Source: Internet
Author: User
Tags chmod curl fpm php and sleep

Method 1: use the shell script to start php and restart nginx and restart the crontab-e scheduled task in linux.

Create a file named 200_ OK .php in PHP (if you want to use curl to access the file)

The code is as follows: Copy code
<? Php
Echo & quot; 200 & quot ";
?>

Create a script vi web_monitor.sh (there is also the command to restart mongoate, and later found that there is a file background process related to shutdown normally, you can directly kill the process ps-ef | grep java)
The content is as follows:

The code is as follows: Copy code
#! /Bin/bash
# Author: Pomelo Lee
# Website: http://www.111cn.net
CheckUrl = "http://www.111cn.net/200_ OK .php"
STATUS_CODE = 'curl-o/dev/null-m 10 -- connect-timeout 10-s-w % {http_code} $ checkurl'
# Echo "$ CheckURL Status Code: t $ STATUS_CODE"
If ["$ STATUS_CODE "! = "200"]; then
/Usr/local/webserver/php/sbin/php-fpm stop
Sleep 1
/Usr/local/webserver/php/sbin/php-fpm start
Sleep 1
Pkill-9 nginx
Sleep 1
/Usr/local/webserver/nginx/sbin/nginx
# Sleep 1
#/Usr/local/tomcat7/bin/shutdown. sh
# Sleep 2
#/Usr/local/tomcat7/bin/startup. sh
Fi

Then set the permission: chmod 755/root/web_monitor.sh

Run the scheduled program: crontab-e

Enter the following content for detection every 5 minutes: */5 *****/root/web_monitor.sh


Method 2: Use curl to obtain the HTTP header. If the 502 status code is found, run the php-fpm restart command.

The code is as follows: Copy code


#! /Usr/bin/php
<! -- $ Url = 'http: // www.111cn.net ';
$ 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, '2014, 502 Bad gateway ')! = False ){
Shell_exec ($ cmd );
Exit ();
}
}
-->

Method 3: automatically restart Lnmp


Save the following code as a. sh file, such as restart502.sh (in Windows, convert it to UNIX format; otherwise, an error may occur .)

The code is as follows: Copy code

#! /Bin/bash
# Created by http://www.111cn.net
Website = http://www.111cn.net # Modify to your own URL
If
Curl-I $ website | grep "HTTP/1.1 502"
Then
/Root/lnmp restart
Fi: put it in/root/restart502.sh.

Get permissions

The code is as follows: Copy code

Add chmod + x/root/restart502.sh to crontab scheduled task (executed every eight minutes)

Crontab-e
*/8 */root/restart502.sh

This completes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.