StackHub Home

Log analytics as a service

Sign Up | Log In | Guides

Making the StackHub daemon a service on Linux

You'll want the StackHub daemon to run as a service on whatever machine is responsible for running it. That means the daemon will start up automatically when the machine boots up, and it also makes it easy for you to stop and start it. There are many ways to set up a service on Linux, but here's a step-by-step guide to get you started.

1. Create the init.d file

We'll create a file called "stackhub" in the "/etc/rc.d/init.d/" directory. You'll need to be root to do this.

#!/bin/sh
# StackHub daemon start/stop script.

# Comments to support chkconfig on RedHat Linux
# chkconfig: 2345 94 16
# description: StackHub daemon

# Comments to support LSB init script conventions
### BEGIN INIT INFO
# Provides: stackhub
# Required-Start: $local_fs $network $remote_fs
# Required-Stop: $local_fs $network $remote_fs
# Default-Start:  2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop stackhub
## END INIT INFO

# Set some defaults
JAVA="/path/to/java/bin/java"

BASE_OPTIONS="-jar stackhub-0.9.jar your-app-key-here"
RUN="$JAVA $BASE_OPTIONS"
MAIN="stackhub-0.9.jar"

basedir="/path/to/install/dir/stackhub"

case `echo "testing\c"`,`echo -n testing` in
    *c*,-n*) echo_n=   echo_c=     ;;
    *c*,*)   echo_n=-n echo_c=     ;;
    *)       echo_n=   echo_c='\c' ;;
esac


cd $basedir
ulimit -n 16384

mode=$1    # start or stop or run
case "$mode" in
  'run')
      echo $RUN
      $RUN
    ;;
  'start')
      echo "Starting stackhub daemon..."
    # Start daemon
      nohup $RUN >> std_stackhub.log 2>&1 &
      # Make lock for RedHat / SuSE
      if test -w /var/lock/subsys
      then
        touch /var/lock/subsys/stackhub
      fi
    ;;

  'stop')
      stackhub_pid=`ps wwwax | grep $MAIN | grep -v grep | head -c 5`
      echo "Killing stackhub with pid $stackhub_pid..."
      kill $stackhub_pid
    sleep 1
    while [ -n ""$stackhub_pid -a "$flags" != aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ]
      do
      [ -z "$flags" ] && echo  "Wait for stackhub to exit"
      flags=a$flags
      sleep 1
      stackhub_pid=`ps wwwax | grep $MAIN | grep -v grep | head -c 5`
      echo "stackhub pid now $stackhub_pid "
    done
      # delete lock for RedHat / SuSE
      if test -f /var/lock/subsys/stackhub
      then
        rm -f /var/lock/subsys/stackhub
      fi
    ;;

  'restart')
    # Stop the service and regardless of whether it was
    # running or not, start it again.
    $0 stop
    $0 start
  ;;

  *)
    # usage
    echo "Usage: $0 start|stop|restart|run"
    exit 1
    ;;
esac

2. Test it out

/etc/rc.d/init.d/stackhub start
/etc/rc.d/init.d/stackhub restart
/etc/rc.d/init.d/stackhub stop

3. Make it part of the startup sequence

/sbin/chkconfig --level 345 stackhub on

4. Permissions

You generally need to be root to do all of the above, but note that the daemon does not require root to run. By default, the daemon uses a high port, and needs little in the way of disk or network access, so you can safely run it as any user.