Page 1 of 1

Ubuntu or CentOS init.d scripts?

Posted: Wed Apr 18, 2012 9:50 pm
by rgeyer
I couldn't find anything by searching so I'm hoping this hasn't been asked for.

Has anyone ever written an init.d script for starting UOX3 on boot for Ubuntu or CentOS?

I'd like to have an init script that launches UOX3 in a screen session, and can restart and terminate that process/screen session. I'm happy to write one and contribute it to the community, but I wanted to make sure I wasn't duplicating effort. :)

Re: Ubuntu or CentOS init.d scripts?

Posted: Wed Apr 18, 2012 10:19 pm
by dragon slayer
I don't think any one has ever made a script like that.

I know some js scripting I'm not a pro or anything hehe just your basic js scripter.

Re: Ubuntu or CentOS init.d scripts?

Posted: Thu Apr 19, 2012 7:55 am
by rgeyer
dragon slayer wrote:I don't think any one has ever made a script like that.

I know some js scripting I'm not a pro or anything hehe just your basic js scripter.
No worries! Looks like I'll be writing one, I'm already well on my way to having it built out.

I'm basically building a pretty complete set of tools to launch UOX3 on CentOS or Ubuntu. Should make things much more approachable for new admins, and devs for that matter. :D

Re: Ubuntu or CentOS init.d scripts?

Posted: Thu Apr 19, 2012 6:25 pm
by Xuri
Great! :)

Re: Ubuntu or CentOS init.d scripts?

Posted: Thu Apr 19, 2012 7:49 pm
by rgeyer
Here she is.. You should only have to change the "UOXDIR" value to where your uox3 executable and script files live, the rest should "just work". I'm gonna be making this even easier to consume though.. Watch this space.. ;)

EDIT: Tweaked to be a proper SysV init script, and call out the process a bit more specifically

Code: Select all

#!/bin/bash
#
# Init file for OpenSSH server daemon
#
# chkconfig: 3 90 10
# description: UOX3 Ultima Offline Experiment Emulator
#
# processname: uox3

. /etc/init.d/functions

UOXDIR=/opt/uox3/shard

# Probably don't need to change anything after this
BINNAME=uox3
BINFILEPATH=$UOXDIR$BINNAME
SCREENLOGFILE=$UOXDIR'screenlog.0'
USERNAME=uox3

exec_as_user() {
  if [ `whoami` == "$USERNAME" ]
  then
    bash -c "$1"
  else
    su $USERNAME -c "$1"
  fi
}

uox_start() {
  if pgrep -f $BINFILEPATH -u $USERNAME > /dev/null
  then
    echo "$BINNAME is already running!"
  else
    echo "Starting $BINNAME in a screen session named $BINNAME"
    cd $UOXDIR
    rm -rf $SCREENLOGFILE
    exec_as_user "screen -dmSL $BINNAME $BINFILEPATH"
    # Wait a bit so that the screenlog file exists
    sleep 2
    COUNTER=0
    while [ 1 -eq 1 ]; do
      grep "UOX: Startup Complete" $SCREENLOGFILE > /dev/null
      if [ $? -eq 0 -o $COUNTER -eq 12 ]
      then
        break
      fi
      sleep 5
      let $COUNTER+1
    done
    if [ $COUNTER -eq 12 ]
    then
      echo "Something went wrong starting $BINNAME. Nuking the universe and giving up"
      exec_as_user "screen -S $BINNAME -X quit"
      exit 1
    else
      exec_as_user "screen -p 0 -S $BINNAME -X eval \"stuff '*'\""
      echo "$BINNAME started successfully"
    fi
  fi
}

uox_stop() {
  if pgrep -f $BINFILEPATH -u $USERNAME > /dev/null
  then
    echo "Stopping $BINNAME"
    exec_as_user "screen -p 0 -S $BINNAME -X eval \"stuff 'Q'\""
    COUNTER=0
    while [ 1 -eq 1 ]; do
      grep "Exiting UOX with no errors..." $SCREENLOGFILE > /dev/null
      if [ $? -eq 0 -o $COUNTER -eq 12 ]
      then
        break
      fi
      sleep 5
      let $COUNTER+1
    done
    if [ $COUNTER -eq 12 ]
    then
      echo "Timed out (5 minutes) waiting for UOX to exit."
      exit 1
    else
      echo "$BINNAME stopped successfully"
    fi
  else
    echo "$BINNAME is not running!"
  fi
}

uox_world_backup() {
  if pgrep -f $BINFILEPATH -u $USERNAME > /dev/null
  then
    echo "Backing up World Files"
    exec_as_user "screen -p 0 -S $BINNAME -X eval \"stuff '@'\""
    COUNTER=0
    while [ 1 -eq 1 ]; do
      tail -10 $SCREENLOGFILE | grep "World save complete." > /dev/null
      if [ $? -eq 0 -o $COUNTER -lt 12 ]
      then
        break
      fi
      sleep 5
      let $COUNTER+1
    done
    if [ $COUNTER -eq 12 ]
    then
      echo "Timed out (5 minutes) waiting for UOX to finish World File backup."
      exit 1
    else
      echo "World File backup completed successfully"
    fi
  else
    echo "$BINNAME is not running!"
  fi
}

uox_status() {
  if pgrep -f $BINFILEPATH -u $USERNAME > /dev/null
  then
    echo "$BINNAME is running"
    exit 0
  else
    echo "$BINNAME is stopped"
    exit 3
  fi
}

case "$1" in
  start)
    uox_start
    ;;
  stop)
    uox_stop
    ;;
  status)
    uox_status
    ;;
  world_backup)
    uox_world_backup
    ;;
  *)
    echo "Usage: $0 {start|stop|status|world_backup}"
    exit 1
    ;;
esac

exit 0

Re: Ubuntu or CentOS init.d scripts?

Posted: Thu Apr 26, 2012 12:11 am
by rgeyer
As promised, here's the "toolkit" which includes this SysV init script, as well as monitoring, backup and restore capabilities, and the ability to run a UOX server on any public cloud provider you like!

viewtopic.php?f=1&t=2395