Generic init script for Fantom based server processes



This is a generic Linux init script for Fantom server processes.
It doesn't use start-stop-daemon so it should work on any Linux distro (tested on Debian and Centos)

It's meant to to be generic and easily configurable.

Basically you would probably want to do this:
  • Create a "fantom" user that we will use to run the server since it's best not to run it as root. (even safer is to have /bin/false set as the shell for the fantom user.
ie: sudo adduser fantom --home /home/fantom --shell /bin/false --disabled-password
  • Download & copy this script in /etc/init.d .
ie: cp initscript.sh /etc/init.d/mycoolserver && chmod +x /etc/init.d/mycoolserver
  • Edit the first few lines of the script and adjust the variables as needed.
ie: replace "mycoolserver" and set FANTOM_HME and FAN_ARGS according to your project and pod names.

You can test it using /etc/init.d/mycoolserver start .. same with stop

The you can make it start automatically at boot (debian)update-rc.d mycoolserver defaults or (redhat)chkconfig --add mycoolserver

The version posted here might not be up to date, so refer to the latest version here:
https://bitbucket.org/tcolar/fantomutils/raw/tip/initscript.sh

I also have a slightly modified version for skyspark here: https://bitbucket.org/tcolar/fantomutils/src/tip/skyspark.sh

#! /bin/bash
# Fantom server init script - Thibaut Colar.

### BEGIN INIT INFO
# Provides:       mycoolserver
# Required-Start: $network
# Required-Stop:  
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Short-Description: mycoolserver
### END INIT INFO

USER="fantom" # User we wil run fantom as
FANTOM_HOME="/home/fantom/fan"
FAN_ARGS="mycoolserverpod"
WORKDIR="/home/fantom"
NAME="mycoolserver"

###### Start script ########################################################

recursiveKill() { # Recursively kill a process and all subprocesses
    CPIDS=$(pgrep -P $1);
    for PID in $CPIDS
    do
        recursiveKill $PID
    done
    sleep 3 && kill -9 $1 2>/dev/null & # hard kill after 3 seconds
    kill $1 2>/dev/null # try soft kill first
}

case "$1" in
      start)
        echo "Starting $NAME ..."
        if [ -f "$WORKDIR/$NAME.pid" ] 
        then 
            echo "Already running according to $WORKDIR/$NAME.pid"
            exit 1
        fi
        export FANTOM_HOME=$FANTOM_HOME
        cd "$WORKDIR"
        /bin/su $USER -p -s /bin/sh -c "$FANTOM_HOME/bin/fan $FAN_ARGS" > "$WORKDIR/$NAME.log" 2>&1 &
        PID=$!
        echo $PID > "$WORKDIR/$NAME.pid"
        echo "Started with pid $PID - Logging to $WORKDIR/$NAME.log" && exit 0
        ;;
      stop)
        echo "Stopping $NAME ..."
        if [ ! -f "$WORKDIR/$NAME.pid" ]
        then
            echo "Already stopped!"
            exit 1
        fi
        PID=`cat "$WORKDIR/$NAME.pid"`
        recursiveKill $PID
        rm -f "$WORKDIR/$NAME.pid"
        echo "stopped $NAME" && exit 0
        ;;
      restart)
        $0 stop
        sleep 1
        $0 start
        ;;
      status)
        if [ -f "$WORKDIR/$NAME.pid" ] 
        then 
            PID=`cat "$WORKDIR/$NAME.pid"`
            if [ "$(/bin/ps --no-headers -p $PID)" ]
            then
                echo "$NAME is running (pid : $PID)" && exit 0
            else
                echo "Pid $PID found in $WORKDIR/$NAME.pid, but not running." && exit 1
            fi
        else
            echo "$NAME is NOT running" && exit 1
        fi
    ;;
      *)
      echo "Usage: /etc/init.d/$NAME {start|stop|restart|status}" && exit 1
      ;;
esac

exit 0


Comments

Add a new Comment