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