Forum: NGINX nginx init script for redhat (rhel5)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
mike (Guest)
on 2009-03-17 04:01
(Received via mailing list)
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 Norbauer <ryan.norbauer@gmail.com>
# Modified:     Geoffrey Grosenbach http://topfunky.com
# Modified:     David Krmpotic http://davidhq.com
# Modified:                     Vishnu Gopal 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
Chris Zimmerman (Guest)
on 2009-03-17 04:21
(Received via mailing list)
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 Norbauer <ryan.norbauer@gmail.com>
# 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
Jim Ohlstein (Guest)
on 2009-03-17 04:24
(Received via mailing list)
I use this one that relies on the start stop daemon --
http://developer.axis.com/download/distribution/ap....

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
mike (Guest)
on 2009-03-17 04:26
(Received via mailing list)
looks like yours also starts /etc/init.d/httpd (most likely apache) as
well?

On Mon, Mar 16, 2009 at 8:11 PM, Chris Zimmerman
mike (Guest)
on 2009-03-17 04:29
(Received via mailing list)
On Mon, Mar 16, 2009 at 8:15 PM, Jim Ohlstein <jim.ohlstein@gmail.com>
wrote:
> I use this one that relies on the start stop daemon --
> 
http://developer.axis.com/download/distribution/ap....

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.
Chris Zimmerman (Guest)
on 2009-03-17 04:34
(Received via mailing list)
Err you could probably remove the httpd lines. That's for cpanel. Sorry.
Cliff Wells (Guest)
on 2009-03-17 04:46
(Received via mailing list)
Cliff Wells (Guest)
on 2009-03-17 04:51
(Received via mailing list)
Oops.  Ignore that.  Here's what I use:

http://wiki.nginx.org/RedHatNginxInitScript

Regards,
Cliff
mike (Guest)
on 2009-03-17 04:59
(Received via mailing list)
On Mon, Mar 16, 2009 at 8:44 PM, Cliff Wells <cliff@develix.com> wrote:
> Oops.  Ignore that.  Here's what I use:
>
> http://wiki.nginx.org/RedHatNginxInitScript

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

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 Norbauer <ryan.norbauer@gmail.com>
# Modified:     Geoffrey Grosenbach http://topfunky.com
# Modified:     David Krmpotic http://davidhq.com
# Modified:                     Vishnu Gopal 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
mike (Guest)
on 2009-03-17 05:03
(Received via mailing list)
On Mon, Mar 16, 2009 at 8:51 PM, mike <mike503@gmail.com> wrote:
> On Mon, Mar 16, 2009 at 8:44 PM, Cliff Wells <cliff@develix.com> wrote:
>> Oops.  Ignore that.  Here's what I use:
>>
>> http://wiki.nginx.org/RedHatNginxInitScript
>
> Got that in plaintext? It wants to copy all the whitespace in my browser too :P

n/m firefox to the rescue.
Cliff Wells (Guest)
on 2009-03-17 05:27
(Received via mailing list)
On Mon, 2009-03-16 at 20:52 -0700, mike wrote:
> On Mon, Mar 16, 2009 at 8:51 PM, mike <mike503@gmail.com> wrote:
> > On Mon, Mar 16, 2009 at 8:44 PM, Cliff Wells <cliff@develix.com> wrote:
> >> Oops.  Ignore that.  Here's what I use:
> >>
> >> http://wiki.nginx.org/RedHatNginxInitScript
> >
> > Got that in plaintext? It wants to copy all the whitespace in my browser too :P
>
> n/m firefox to the rescue.


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

Cliff
mike (Guest)
on 2009-03-17 05:34
(Received via mailing list)
i got it.

thanks - this seems to work even better than mine.

/me adds it to his installer
mike (Guest)
on 2009-03-17 05:36
(Received via mailing list)
Now I just need one for Ubuntu with the same amount of intelligence. :p
mike (Guest)
on 2009-03-17 08:10
(Received via mailing list)
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.
Jeroen Steggink - CMS (Guest)
on 2009-03-17 08:40
(Received via mailing list)
Hello,

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

Jeroen
mike (Guest)
on 2009-03-17 09:58
(Received via mailing list)
i have updated the wiki.

On Tue, Mar 17, 2009 at 12:29 AM, Jeroen Steggink - CMS
Jean-Philippe Moal (Guest)
on 2009-03-17 10:51
(Received via mailing list)
mike a écrit :
> Now I just need one for Ubuntu with the same amount of intelligence. :p
>
> On Mon, Mar 16, 2009 at 9:24 PM, mike <mike503@gmail.com> 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/...

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.
mike (Guest)
on 2009-03-17 18:32
(Received via mailing list)
On Tue, Mar 17, 2009 at 2:43 AM, Jean-Philippe Moal
<skateinmars@skateinmars.net> wrote:

> You can use debian/ubuntu one :
> 
http://svn.debian.org/wsvn/collab-maint/deb-maint/...
>
> 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 :))
This topic is locked and can not be replied to.