FastCGI Init Setup

by Scott, Revised on November 5, 2010 (LinuxWeb Serving)

Provide an Init Script (myuser)

Note: starts as non-root myuser, needs to be a sudoer

#!/bin/sh

USER=myuser
APP_PATH=/root/path/to/app/
SCRIPT_PATH=script/app_fastcgi.pl
SOCKET_PATH=/tmp/app.socket
PID_PATH=/tmp/app.pid

case $1 in
    start)
        echo -n "Starting server..."

        PID=`cat $PID_PATH`
        if [ -n "$PID" ]
        then
             echo "Server already started"
        else
            cd $APP_PATH
            exec sudo -u $USER $SCRIPT_PATH -l $SOCKET_PATH -p $PID_PATH -n 5 -d
            echo

            PID=`cat $PID_PATH`
            if [ -n "$PID" ]
            then
                echo "Started"
            else
                echo "Start failed"
            fi
         fi
         ;;

    stop)
        echo -n "Stopping server: "
        PID=`cat $PID_PATH`
        if [ -n "$PID" ]
        then
            echo -n kill $PID
            kill $PID
            echo
            unlink $SOCKET_PATH
       else
             echo "Server not running"
        fi
        ;;

    restart)
        echo -n "Restarting server: "
        PID=`cat $PID_PATH`
        if [ -n "$PID" ]
        then
            echo -n kill HUP $PID
            kill -HUP $PID
            sleep 3
           echo
        else
            echo "Server not running"
        fi
        ;;

    *)
        echo "Usage: daemon { stop | start | restart }"
        exit 1
        ;;
esac

Make Executable (myuser)

chmod 700 app_init.sh

Link to /etc/init.d Directory (root)

cd /etc/init.d
ln -s /location/to/app_init.sh app_init.sh

Setup Run Levels for Init Script (root)

update-rc.d app_init.sh start 95 2 3 4 5 . stop 95 0 1 6 .

Manually Start (myuser) or Reboot

/etc/init.d/app_init.sh start

Tags: LinuxWeb Serving