Sunday, December 12, 2010

RHEL/Centos init script for a Catalyst FCGI server

UPDATE I've made a Gist
#!/bin/sh
#
# chkconfig:   345 86 14 
# description:  MyApp FCGI server 
# pidfile:     /var/run/myapp/pid

PATH=/sbin:/bin:/usr/bin:/usr/sbin

# Source function library.
. /etc/rc.d/init.d/functions

# Get config.
test -f /etc/sysconfig/network && . /etc/sysconfig/network

# Check that we are root ... so non-root users stop here
[ `id -u` = 0 ] || exit 1

# Check that networking is up.
[ "${NETWORKING}" = "yes" ] || exit 0

APP_HOME=/opt/myapp
RUN_DIR=/var/run/myapp
USER=myapprun

PID_FILE=$RUN_DIR/pid
SOCKET_FILE=$RUN_DIR/socket
SUBSYS_FILE=/var/lock/subsys/myapp

SCRIPT=$APP_HOME/script/myapp_fastcgi.pl
SCRIPT_ARGS="-l $SOCKET_FILE -p $PID_FILE -d"

# Check that the script is executable
[ -x $SCRIPT ] || exit 5

start(){
    echo -n $"Starting myapp: "

    daemon --user $USER $SCRIPT "$SCRIPT_ARGS >/dev/null 2>&1"
    RETVAL=$?
    echo
    touch $SUBSYS_FILE
    return $RETVAL
}

stop(){
    echo -n $"Stopping myapp: "
    killproc -p $PID_FILE myapp
    RETVAL=$?
    echo
    rm -f $SUBSYS_FILE
    return $RETVAL
}

restart(){
    stop
    start
}

condrestart(){
    [ -e $SUBSYS_FILE ] && restart
    return 0
}


# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status -p $PID_FILE myapp
        ;;
    restart)
        restart
        ;;
    condrestart)
        condrestart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart}"
        RETVAL=1
esac

exit $RETVAL

4 comments:

  1. Under the line
    > SCRIPT_ARGS="-l $RUN_DIR/socket -p $RUN_DIR/pid -d"

    might as well use the socket and run variables. So it would look something like

    > SCRIPT_ARGS="-l $SOCKET_FILE -p $PID_FILE -d"

    ReplyDelete
  2. Yes... it was a mispaste ;) Thank you Jason!

    ReplyDelete
  3. Pass daemon the --pidfile option so that __pids_var_run() can find the application's pid file. This will keep you from starting multiple copies of your application. Putting your pid in the default location (/var/run/BASENAME OF APP.pid) so that it will be found by default would probably be cleaner.

    Also, look into the --proc_title option to your Catalyst application. If you run multiple instances of your application (as we do) it's a lot easier to manage the application when the main process has a unique name.

    ReplyDelete
    Replies
    1. this is an old version of the script... I should update it ;)

      Delete