Nginx init script for redhat (rhel5)

for the most part, it works fine.

however, sometimes the pid file disappears (?)
also, “restart” does not work if the daemon is not running already. it
should handle that gracefully.

anyone have a better one? i also have the same issue with “restart” on
my ubuntu version as well…

#!/bin/sh

Init file for nginx

chkconfig: 2345 55 25

description: Nginx web server

processname: nginx

config: /usr/local/nginx/nginx.conf

pidfile: /usr/local/nginx/nginx.pid

Description: Startup script for nginx webserver on Debian. Place in

/etc/init.d and

run ‘sudo update-rc.d nginx defaults’, or use the appropriate command

on your

distro. For CentOS/Redhat run: ‘/sbin/chkconfig --add nginx’

Author: Ryan N. [email protected]

Modified: Geoffrey Grosenbach http://topfunky.com

Modified: David K. http://davidhq.com

Modified: Vishnu G. http://vish.in

set -e

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC=“nginx daemon”
NAME=nginx
DAEMON=/usr/sbin/nginx
CONFIGFILE=/etc/nginx/nginx.conf
PIDFILE=/var/run/nginx.pid
SCRIPTNAME=/etc/init.d/nginx

Gracefully exit if the package has been removed.

test -x $DAEMON || exit 0

d_start() {
$DAEMON -c $CONFIGFILE || echo -en “\n already running”
}

d_stop() {
kill -QUIT cat $PIDFILE || echo -en “\n not running”
}

d_reload() {
kill -HUP cat $PIDFILE || echo -en “\n can’t reload”
}

case “$1” in
start)
echo -n “Starting $DESC: $NAME”
d_start
echo “.”
;;
stop)
echo -n “Stopping $DESC: $NAME”
d_stop
echo “.”
;;
reload)
echo -n “Reloading $DESC configuration…”
d_reload
echo “.”
;;
restart)
echo -n “Restarting $DESC: $NAME”
d_stop
# One second might not be time enough for a daemon to stop,
# if this happens, d_start will fail (and dpkg will break if
# the package is being upgraded). Change the timeout if needed
# be, or change d_stop to have start-stop-daemon use --retry.
# Notice that using --retry slows down the shutdown process
somewhat.
sleep 1
d_start
echo “.”
;;
*)
echo “Usage: $SCRIPTNAME {start|stop|restart|reload}” >&2
exit 3
;;
esac

exit 0

Better probably, not. This is what I use. At least it works.

#! /bin/sh

Description: Startup script for nginx webserver on Debian. Place in

/etc/init.d and

run ‘sudo update-rc.d nginx defaults’, or use the appropriate command

on your

distro.

#chkconfig: 2345 55 25

Author: Ryan N. [email protected]

Modified: Geoffrey Grosenbach http://topfunky.com

set -e

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC=“nginx daemon”
NAME=nginx
DAEMON=/usr/local/sbin/$NAME
CONFIGFILE=/etc/nginx/nginx.conf
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

Gracefully exit if the package has been removed.

test -x $DAEMON || exit 0

d_start() {
$DAEMON -c $CONFIGFILE || echo -n " already running"
}

d_stop() {
kill -15 cat $PIDFILE || echo -n " not running"
}

d_reload() {
kill -15 cat $PIDFILE || echo -n " can’t reload"
}

case “$1” in
start)
echo -n “Starting $DESC: $NAME”
d_start
echo “.”
/etc/init.d/httpd start
;;
stop)
echo -n “Stopping $DESC: $NAME”
d_stop
echo “.”
/etc/init.d/httpd stop
;;
reload)
echo -n “Reloading $DESC configuration…”
d_reload
echo “reloaded.”
/etc/init.d/httpd restart
;;
restart)
echo -n “Restarting $DESC: $NAME”
d_stop
# One second might not be time enough for a daemon to stop,
# if this happens, d_start will fail (and dpkg will break if
# the package is being upgraded). Change the timeout if needed
# be, or change d_stop to have start-stop-daemon use --retry.
# Notice that using --retry slows down the shutdown process
somewhat.
sleep 1
d_start
echo “.”
/etc/init.d/httpd restart
;;
*)
echo “Usage: $SCRIPTNAME {start|stop|restart|force-reload}” >&2
exit 3
;;
esac

exit 0

I use this one that relies on the start stop daemon –
http://developer.axis.com/download/distribution/apps-sys-utils-start-stop-daemon-IR1_9_18-2.tar.gz.

After its unpacked:

$ cd apps/sys-utils/start-stop-daemon-IR1_9_18-2/
$gcc start-stop-daemon.c -o start-stop-daemon

Then copy it to /usr/local/sbin or wherever suits your fancy.

I have no issues with the pid file but it also ignores a restart request
if not running.

#! /bin/sh

BEGIN INIT INFO

Provides: nginx

Required-Start: $all

Required-Stop: $all

Default-Start: 2 3 4 5

Default-Stop: 0 1 6

Short-Description: starts the nginx web server

Description: starts nginx using start-stop-daemon

END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/sbin/nginx
NAME=nginx
DESC=nginx

test -x $DAEMON || exit 0

Include nginx defaults if available

if [ -f /etc/default/nginx ] ; then
. /etc/default/nginx
fi

set -e

case “$1” in
start)
echo -n "Starting $DESC: "
start-stop-daemon --start --quiet --pidfile
/usr/local/nginx/logs/$NAME.pid
–exec $DAEMON – $DAEMON_OPTS
echo “$NAME.”
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --quiet --pidfile
/usr/local/nginx/logs/$NAME.pid
–exec $DAEMON
echo “$NAME.”
;;
restart|force-reload)
echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --pidfile
/usr/local/nginx/logs/$NAME.pid --exec $DAEMON
sleep 1
start-stop-daemon --start --quiet --pidfile
/usr/local/nginx/logs/$NAME.pid --exec $DAEMON –
$DAEMON_OPTS
echo “$NAME.”
;;
reload)
echo -n "Reloading $DESC configuration: "
start-stop-daemon --stop --signal HUP --quiet --pidfile
/usr/local/nginx/logs/$NAME.pid
–exec $DAEMON
echo “$NAME.”
;;
*)
N=/etc/init.d/$NAME
echo “Usage: $N {start|stop|restart|force-reload}” >&2
exit 1
;;
esac

exit 0

On Mon, Mar 16, 2009 at 8:15 PM, Jim O. [email protected]
wrote:

I use this one that relies on the start stop daemon –
http://developer.axis.com/download/distribution/apps-sys-utils-start-stop-daemon-IR1_9_18-2.tar.gz.

yeah … i use one relatively similar for ubuntu. but i’d rather use
out-of-the-box rhel5/epel/rpmfusion supportable stuff.

when it comes down to it if i have to figure out bash script hacking
for it, so be it. just thought i’d ask.

the odd thing is when the pid file can’t be found. that one will
require a killall -9 nginx i suppose. and the restart should be taken
care of as well then too.

looks like yours also starts /etc/init.d/httpd (most likely apache) as
well?

On Mon, Mar 16, 2009 at 8:11 PM, Chris Z.

http://wiki.nginx.org/RedHatInitScript

Err you could probably remove the httpd lines. That’s for cpanel. Sorry.

Oops. Ignore that. Here’s what I use:

Regards,
Cliff

On Mon, Mar 16, 2009 at 8:51 PM, mike [email protected] wrote:

On Mon, Mar 16, 2009 at 8:44 PM, Cliff W. [email protected] wrote:

Oops. Â Ignore that. Â Here’s what I use:

Red Hat NGINX Init Script | NGINX

Got that in plaintext? It wants to copy all the whitespace in my browser too :stuck_out_tongue:

n/m firefox to the rescue.

On Mon, Mar 16, 2009 at 8:44 PM, Cliff W. [email protected] wrote:

Oops. Â Ignore that. Â Here’s what I use:

Red Hat NGINX Init Script | NGINX

Got that in plaintext? It wants to copy all the whitespace in my browser
too :stuck_out_tongue:

For sharing sake this seems to be pretty foolproof too. Should kill
off rogue instances that don’t match the pidfile etc.

even “reload” will restart it if it isn’t actually running.

#!/bin/sh

Init file for nginx

chkconfig: 2345 55 25

description: Nginx web server

processname: nginx

config: /usr/local/nginx/nginx.conf

pidfile: /usr/local/nginx/nginx.pid

Description: Startup script for nginx webserver on Debian. Place in

/etc/init.d and

run ‘sudo update-rc.d nginx defaults’, or use the appropriate command

on your

distro. For CentOS/Redhat run: ‘/sbin/chkconfig --add nginx’

Author: Ryan N. [email protected]

Modified: Geoffrey Grosenbach http://topfunky.com

Modified: David K. http://davidhq.com

Modified: Vishnu G. http://vish.in

set -e

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC=“nginx daemon”
NAME=nginx
DAEMON=/usr/sbin/nginx
CONFIGFILE=/etc/nginx/nginx.conf
PIDFILE=/var/run/nginx.pid
SCRIPTNAME=/etc/init.d/nginx

Gracefully exit if the package has been removed.

test -x $DAEMON || exit 0

d_start() {
$DAEMON -c $CONFIGFILE || echo -en “ERROR: Could not start.\n”
}

d_stop() {
if [ -f $PIDFILE ]; then
kill -QUIT cat $PIDFILE || echo -en “INFO: not running.\n”
fi

force kill

pgrep -f “nginx:” | xargs --no-run-if-empty kill -9 2>/dev/null
rm -f $PIDFILE
}

d_reload() {
if [ -f $PIDFILE ]; then
kill -HUP cat $PIDFILE || echo -en “ERROR: can’t reload.\n”
else
echo -en “NOTICE: can’t reload, force (re)starting\n”
$SCRIPTNAME restart
fi
}

case “$1” in
start)
echo -en “Starting $DESC: $NAME\n”
d_start
;;
stop)
echo -en “Stopping $DESC: $NAME\n”
d_stop
;;
reload)
echo -en “Reloading $DESC configuration…\n”
d_reload
;;
restart)
echo -en “Restarting $DESC: $NAME\n”
d_stop
sleep 1
d_start
;;
*)
echo “Usage: $SCRIPTNAME {start|stop|restart|reload}” >&2
exit 3
;;
esac

exit 0

On Mon, 2009-03-16 at 20:52 -0700, mike wrote:

On Mon, Mar 16, 2009 at 8:51 PM, mike [email protected] wrote:

On Mon, Mar 16, 2009 at 8:44 PM, Cliff W. [email protected] wrote:

Oops. Ignore that. Here’s what I use:

Red Hat NGINX Init Script | NGINX

Got that in plaintext? It wants to copy all the whitespace in my browser too :stuck_out_tongue:

n/m firefox to the rescue.

If you click the link right above the script, it should offer to
download it for you.

Cliff

Now I just need one for Ubuntu with the same amount of intelligence. :stuck_out_tongue:

i got it.

thanks - this seems to work even better than mine.

/me adds it to his installer

Hello,

I had the same problem. After adding ‘sleep 1’ it works.

Jeroen

i have updated the wiki.

On Tue, Mar 17, 2009 at 12:29 AM, Jeroen Steggink - CMS

I changed this minorly

restart() {
configtest || return $?
stop
sleep 1
start
}

to include that sleep 1

it seems sometimes “stop” doesn’t clean things up fast enough perhaps.
since then i have had no issues

[root@mia32217www002 build]# service nginx restart
2009/03/16 22:48:01 [info] 2934#0: the configuration file
/etc/nginx/nginx.conf syntax is ok
2009/03/16 22:48:01 [info] 2934#0: the configuration file
/etc/nginx/nginx.conf was tested successfully Stopping nginx: [ OK ]
Starting nginx: [ OK ]
[root@mia32217www002 build]# service nginx restart
2009/03/16 22:48:03 [info] 2957#0: the configuration file
/etc/nginx/nginx.conf syntax is ok
2009/03/16 22:48:03 [info] 2957#0: the configuration file
/etc/nginx/nginx.conf was tested successfully Stopping nginx: [ OK ]
Starting nginx: [ OK ]
[root@mia32217www002 build]# service nginx restart
2009/03/16 22:48:03 [info] 2980#0: the configuration file
/etc/nginx/nginx.conf syntax is ok
2009/03/16 22:48:03 [info] 2980#0: the configuration file
/etc/nginx/nginx.conf was tested successfully Stopping nginx: [ OK ]
Starting nginx:
[root@mia32217www002 build]# service nginx restart
2009/03/16 22:49:04 [info] 3052#0: the configuration file
/etc/nginx/nginx.conf syntax is ok
2009/03/16 22:49:04 [info] 3052#0: the configuration file
/etc/nginx/nginx.conf was tested successfully Stopping nginx: [FAILED]
Starting nginx: [ OK ]

^^ that fourth time it failed…

sleep 1 so far seems to be foolproof. my provider probably hates me
ive been restarting it over and over.

mike a écrit :

Now I just need one for Ubuntu with the same amount of intelligence. :stuck_out_tongue:

On Mon, Mar 16, 2009 at 9:24 PM, mike [email protected] wrote:

i got it.

thanks - this seems to work even better than mine.

/me adds it to his installer

You can use debian/ubuntu one :
http://svn.debian.org/wsvn/collab-maint/deb-maint/nginx/trunk/debian/init.d?op=file&rev=0&sc=0

It works fine for me with a “custom” nginx install, you just have to
edit the
path to the daemon and if necessary add a var for the PID file.

On Tue, Mar 17, 2009 at 2:43 AM, Jean-Philippe M.
[email protected] wrote:

You can use debian/ubuntu one :
http://svn.debian.org/wsvn/collab-maint/deb-maint/nginx/trunk/debian/init.d?op=file&rev=0&sc=0

It works fine for me with a “custom” nginx install, you just have to edit the
path to the daemon and if necessary add a var for the PID file.

I might give it a shot. I think I’d rather port Cliff’s. It does a
config test on start/reload/restart, which is neat.

I’m actually using one almost identical to yours except yours has “||
true” at the end of each line:

–exec $DAEMON – $DAEMON_OPTS || true

So I don’t think it will be much different.

Or, if someone else on the list wants to take Cliff’s and port it to
Ubuntu using start-stop-daemon (ideally) I’ll Paypal you some cash.

(Note: payment will be done after I test it thoroughly for all
functionality in place :))