init.d example for Fantom

Wednesday, 15 August 2012 8:31am

Update 27 Feb 2013 – While this should still be valid advice – it’s been superseded by a more comprehensive guide covering the whole process of getting Fantom up and running on Debian:

http://www.andyfrank.com/blog/2013/02/fantom-linode-guide


Been meaning to post this for awhile. Below is the init.d script I use to run Fantom on my personal sites. I'm running Debian 6 – but should be straightforward to tweak for other distros. Might need to apt-get a few things like pgrep - can't remember what came out of the box.

A few caveats:

  1. I always install and run each "app" using a dedicated user with its own Fantom runtime. I find this really simplifies security, process management, and versioning. This example uses the fan user.

  2. This script should properly block until process cleanly exits. But don't have a good way to know when start is complete - so just waits a few seconds. You can tweak depending on your app.

  3. This script redirects stdout and stderr to the same log file. You can tweak as necessary.

  4. I'm using a script (boot.fan) to launch my site. But you can launch Wisp or another main directly from bash string.

Example server layout:

~/               User home directory
  fantom/        Fantom runtime
    bin/
    lib/
    ...
  dist/          App path env
    fan.props    Empty fan.props to force PathEnv
    lib/         PathEnv lib for App
    ...

Example init.d script for Fantom:

#! /bin/bash
# /etc/init.d/example

FIND_PID="pgrep -u fan java"
STDOUT=/home/fan/var/stdout.log

case "$1" in
  start)
    PID=$( $FIND_PID )
    if [ -f $PID ]; then
      echo "Starting example.com..."
      sudo -u fan bash -c  'cd ~/dist; ~/fantom/bin/fan boot.fan &' &> $STDOUT
      sleep 3
    else
      echo "example.com is already running"
    fi
    ;;
  stop)
    echo "Stopping example.com..."
    PID=$( $FIND_PID )
    if [ -f $PID ]; then
      echo "example.com is not running"
    else
      kill $PID
      while kill -0 $PID 2> /dev/null; do
        sleep 0.5
      done
      echo "stopped"
    fi
    ;;
  status)
    PID=$( $FIND_PID )
    if [ -f $PID ]; then
      echo "example.com is not running"
    else
      echo "example.com is running"
    fi
    ;;
  restart)
    $0 stop
    $0 start
    ;;
  *)
    echo "Usage: /etc/init.d/example {start|stop|restart|status}"
    exit 1
    ;;
esac

exit 0