Welcome! Log In Create A New Profile

Advanced

Make a auto restart script when nginx crashes

Posted by smc1979 
Make a auto restart script when nginx crashes
January 24, 2011 11:06PM
Hello everyone.

On my site PcWintech.com I am using Nginx. I have been very impressed with it. Currently using 0.8.53

My site is hosted with dreamhost. And is on a Virtual Private server.

3 times in the last month and many more times before that nginx has crashed. I get 502 bad gateway. I check the resources and they are not being touched. I have to restart the server to get it going. Only takes about 20 sec.

So I asked dreamhost support to look at the logs for me since I cant view them.

Here is what they gave me

[code]
I'd be happy to look into this for you. You don't have access to the
nginx logs, so we checked it, and found the following from around the
most likely time your service crashed:

101884:41.104.104.32 - - [24/Jan/2011:03:28:50 -0800] "HEADD
/files/setups/cleanmem_v1.7.0_setup.exe " 400 0 "-" "-"

Notice your usage graphs: at around 3:30am it drops and doesn't come back
until around the time you restarted it at 10:30am.

It appears that nginx was trying to run that file which in turn killed
it.
[/code]

That should be just someone downloading my exe from my site. Not sure why they said it was trying to run the file.
However they havent given me any reason why nginx is crashing. My site was down for 7 hours before I realized it.

So I want to make a script that will run on the server checking if nginx is running and if it isnt to start it. This should help when it crashes.

I know I should find the problem first, but to me this is a good script to have no matter what.

Problem is I am a windows programmer. If this was in Windows I could make the script in 5 min. So any help, or advice would be great :-)

-Shane
Re: Make a auto restart script when nginx crashes
January 26, 2011 03:13PM
This should work...

[code]
#path to pgrep command
PGREP="/usr/bin/pgrep"

# Httpd daemon name,
HTTPD="nginx"

# find httpd pid
$PGREP ${HTTPD}

if [ $? -ne 0 ] # if nginx not running
then
# restart nginx
$RESTART
fi
[/code]
Re: Make a auto restart script when nginx crashes
January 26, 2011 04:25PM
Thanks a ton. I will give it a try :-)

-Shane
Re: Make a auto restart script when nginx crashes
January 26, 2011 04:30PM
You are welcome :)
Re: Make a auto restart script when nginx crashes
October 17, 2011 05:16AM
where you will enter this code
Re: Make a auto restart script when nginx crashes
October 17, 2011 06:59AM
I am not sure that it's nginx crash. If nginx would crash, you would not be able to get the 502 Bad Gateway response. This sounds more like the FastCGI backend has crashed, either PHP-FPM or PHP-CGI or anything else you've configured. In either case, it's better to resolve this issue by analyzing the logs and seeing why the backend crashes (if it's the backend), rather than restarting the server, which would not change the situation.

Andrejs
Re: Make a auto restart script when nginx crashes
October 17, 2011 01:35PM
Surprised to see reply's to my old thread.
This has been fixed a long time ago. PHP was crashing with nginx. Made a script to test is php is running every one min. If it isnt it restarts.

Problem fixed :-)
Re: Make a auto restart script when nginx crashes
October 17, 2011 04:07PM
psl send me code thanks
Re: Make a auto restart script when nginx crashes
October 17, 2011 04:16PM
PGREP="/usr/bin/pgrep"
PHPD="php5.cgi"

$PGREP ${PHPD}

if [ $? -ne 0 ] # No php processes, restart
then
# restart php
sudo /etc/init.d/nginx stopphp
sudo /etc/init.d/nginx startphp
fi
Re: Make a auto restart script when nginx crashes
October 17, 2011 04:19PM
I do not know much. where you should add these lines .. and service only to control nginx, I'm using apache for php



Edited 1 time(s). Last edit at 10/17/2011 04:24PM by codetr.
Re: Make a auto restart script when nginx crashes
October 17, 2011 04:33PM
I wont be much help. I had tech support at dreamhost help me get it going.

Its just a .pl script that runs via cron every 1 min.
Re: Make a auto restart script when nginx crashes
October 17, 2011 04:34PM
smc1979 Wrote:
-------------------------------------------------------
> I wont be much help. I had tech support at
> dreamhost help me get it going.
>
> Its just a .pl script that runs via cron every 1
> min.

tanks bro
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 252
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready