Recently, the newly built Amazon EC2 Server, which deploys a static web, initiates an nginx proxy. Recently found a problem:
Nginx process interval time on the inexplicable hang off, and then there is the site can not open the dilemma.
To prevent this problem from happening again, use the following method to do the Nginx daemon.
1. Script, monitor the nginx process, if it hangs, restart, otherwise do not intervene.
Under the/data/work/scripts directory, create a restart_nginx.sh file that reads as follows:
#查找nginx进程, exclude the process number generated by the grep command, and awk prints the 2nd column value (that is, the process number) assigned to the PID variable pid=`PSAux |grepNginx |grep-Vgrep|awk '{print $}'`
#记录当前时间dat=`Date '+%y-%m-%d%h:%m:%s'' #输出当前时间Echo$dat
#输出进程号Echo$pid # True if the length of the string is greater than 0 (the process number is not empty)if[-N"$pid" ] Then{
# show that the process is still alive, no processingEcho===========alive!================}Else
#否则进程挂了, reboot requiredEcho===========shutdown!start==============/opt/tengine/sbin/Nginxfi
2. Grant restart_nginx.sh File executable permissions
chmod u+x Restart_nginx. SH
3. Edit the Linux timer, add timed tasks, execute restart_nginx.sh script every 2 minutes
Crontab-e
Paste the following content:
*/2 * * * * */data/work/scripts/restart_nginx. sh >>/data/work/scripts/restart_nginx.log
4. Restart the timer
/etc/init.d/crond restart
5. Check the status of the current Nginx process first
Found four processes related to Nginx.
6. Check the timer log:
Indicates that the process is normal at this time.
6. In order to test the Nginx hangs can automatically restart, we manually kill Nginx
PS grep ' Nginx ' grep grep awk ' {print $} ' Xargs Kill -9
At this time Nginx has been killed, we wait two minutes to see if the process can be automatically restored under the timer.
At this point, the current process has started!
How Linux implements process monitoring and guarding