Ubuntu or CentOS init.d scripts?

Forum where anything UOX3-related goes - including, but not limited to: newbie-support, ideas, general questions, comments, etc and-so-forth.
Post Reply
rgeyer
UOX3 Newbie
Posts: 7
Joined: Tue Apr 10, 2012 4:53 am
Has thanked: 0
Been thanked: 0

Ubuntu or CentOS init.d scripts?

Post 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. :)
dragon slayer
UOX3 Guru
Posts: 776
Joined: Thu Dec 21, 2006 7:37 am
Has thanked: 4 times
Been thanked: 26 times

Post 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.
rgeyer
UOX3 Newbie
Posts: 7
Joined: Tue Apr 10, 2012 4:53 am
Has thanked: 0
Been thanked: 0

Post 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
User avatar
Xuri
Site Admin
Posts: 3704
Joined: Mon Jun 02, 2003 9:11 am
Location: Norway
Has thanked: 48 times
Been thanked: 8 times
Contact:

Post by Xuri »

Great! :)
-= Ho Eyo He Hum =-
rgeyer
UOX3 Newbie
Posts: 7
Joined: Tue Apr 10, 2012 4:53 am
Has thanked: 0
Been thanked: 0

Post 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
Last edited by rgeyer on Thu Apr 26, 2012 12:14 am, edited 1 time in total.
rgeyer
UOX3 Newbie
Posts: 7
Joined: Tue Apr 10, 2012 4:53 am
Has thanked: 0
Been thanked: 0

Post 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
Post Reply