Debian init.d script for fcgi

August 4, 2010

Here's a debian init.d script for fcgi.

#! /bin/sh
#
# fcgi-hg     Startup script for the nginx HTTP Server
#
# chkconfig: - 84 15
# description: Loading php-cgi using spawn-cgi
#              HTML files and CGI.
#
# Author:  Ryan Norbauer <ryan.norbauer@gmail.com>
# Modified:     Geoffrey Grosenbach http://topfunky.com
# Modified:     David Krmpotic http://davidhq.com
# Modified:     Kun Xi http://kunxi.org
# Modified:     http://drumcoder.co.uk/
DAEMON=/usr/local/bin/spawn-fcgi
FCGIHOST=127.0.0.1
FCGIPORT=9085
FCGIUSER=www-data
FCGIGROUP=www-data
FCGIAPP=/var/hg/cgi-bin/hgweb.cgi
PIDFILE=/var/run/fcgi-hg.pid
DESC="HG in FastCGI mode"

# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0
test -x $FCGIAPP || exit 0

start() {
  $DAEMON -a $FCGIHOST -p $FCGIPORT -u $FCGIUSER -g $FCGIGROUP -f $FCGIAPP -P $PIDFILE 2> /dev/null || echo -en "\n already running"
}

stop() {
  kill -QUIT `cat $PIDFILE` || echo -en "\n not running"
}

restart() {
  kill -HUP `cat $PIDFILE` || echo -en "\n can't reload"
}

case "$1" in
  start)
    echo -n "Starting $DESC: "
    start
  ;;
  stop)
    echo -n "Stopping $DESC: "
    stop
  ;;
  restart|reload)
    echo -n "Restarting $DESC: "
    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
    start
  ;;
  *)
    echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
    exit 3
  ;;
esac

exit $?

Autostart

To autostart this script at debian bootup, run the following command. This assumes your script is at /etc/init.d/fcgi-hg

# update-rc.d fcgi-hg defaults

You can use the following command to stop the service auto-starting:

# update-rc.d -f fcgi-hg remove

-f forces the removal of the symlinks from /etc/rc?.d even though there is still the destination file in /etc/init.d. The output of this command is:

Removing any system startup links for /etc/init.d/apache2 ...
  /etc/rc0.d/K09apache2
  /etc/rc1.d/K09apache2
  /etc/rc2.d/S91apache2
  /etc/rc3.d/S91apache2
  /etc/rc4.d/S91apache2
  /etc/rc5.d/S91apache2
  /etc/rc6.d/K09apache2

The K stands for kill and the S for start.

If the process is installed using the package manager (ie it's Apache rather than an init.d script you're written yourself) then it will be autostarted again when the process is next updated. To avoid this, run

# update-rc.d fcgi-hg stop 80 0 1 2 3 4 5 6 .

This command outputs:

update-rc.d: warning: apache2 start runlevel arguments (none) do not match LSB Default-Start values (2 3 4 5)
update-rc.d: warning: apache2 stop runlevel arguments (0 1 2 3 4 5 6) do not match LSB Default-Stop values (0 1 6)
 Adding system startup for /etc/init.d/apache2 ...
   /etc/rc0.d/K80apache2 -> ../init.d/apache2
   /etc/rc1.d/K80apache2 -> ../init.d/apache2
   /etc/rc2.d/K80apache2 -> ../init.d/apache2
   /etc/rc3.d/K80apache2 -> ../init.d/apache2
   /etc/rc4.d/K80apache2 -> ../init.d/apache2
   /etc/rc5.d/K80apache2 -> ../init.d/apache2
   /etc/rc6.d/K80apache2 -> ../init.d/apache2

References

Tags: fcgi init.d