Author Topic: /etc/init.d scripts  (Read 1007 times)

Offline quixadhal

  • BFF
  • ***
  • Posts: 618
    • View Profile
    • A Waste of Time
/etc/init.d scripts
« on: August 19, 2015, 02:23:20 AM »
Hey guys,

This hasn't been fully tested yet, but I figured I'd share it anyways.  One of the annoying facts of life for any MUD is that the machine it runs on has to reboot every so often.  When it does, it's often when you're not there due to a power outage.

UNIX systems, of course, have ways to automatically start things at boot time.  If you're on an older system that uses /etc/rc and /etc/rc.local, good for you!  You already know how to do stuff, and don't need this.  If you're running linux, almost every distro has switched to SYSV init by now, and a few have some new annoyance that's even worse.

If you have /etc/rc.d directories and /etc/init.d, this is for you.

We have three scripts here.  The main one is called bloodlines, because that's my MUD.  Rename it as you like, and edit a few things like the paths to fit your environment.  The second two are startup scripts for the driver and the addr_server, because I wanted to keep them isolated from each other.  You could probably just merge them into the main script, and I might do so later.

Code: (bloodlines) [Select]
#!/bin/sh
#
### BEGIN INIT INFO
# Provides:          bloodlines
# Required-Start:    $network $local_fs $remote_fs $syslog $named $time
# Required-Stop:     $network $local_fs $remote_fs $syslog $named $time
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: init-Script for FluffOS MUD driver
### END INIT INFO
#

USERNAME=bloodlines
APORT=4990
MUDDIR=/home/bloodlines
PATH=$MUDDIR/bin:/bin:/usr/bin:/sbin:/usr/sbin
CONFIG=$MUDDIR/bin/mudos.cfg
STARTDIR=$MUDDIR/bin
LOGFILE=`/bin/date "+$MUDDIR/bin/driver_log.%y%m%d-%H%M%S"`

DAEMON=$MUDDIR/bin/driver
PIDFILE=$MUDDIR/bin/driver.pid
SCRIPT=$MUDDIR/bin/start.driver

ADAEMON=$MUDDIR/bin/addr_server
APIDFILE=$MUDDIR/bin/addr_server.pid
ASCRIPT=$MUDDIR/bin/start.addr_server

test -x $DAEMON || exit 0
test -x $ADAEMON || exit 0

case "$1" in
    start)
        echo "Starting MUD Server: Bloodlines"
        /sbin/start-stop-daemon --start --quiet --chuid $USERNAME:users --chdir $STARTDIR --background --pidfile $PIDFILE --exec $ASCRIPT
        # Small delay to ensure it's alive
        sleep 2
        /sbin/start-stop-daemon --start --quiet --chuid $USERNAME:users --chdir $STARTDIR --background --pidfile $PIDFILE --exec $SCRIPT
        ;;
    stop)
        echo -n "Stopping MUD Server: Bloodlines, driver"
        if start-stop-daemon --quiet --stop --signal 0 --pidfile $PIDFILE --name driver --user $USERNAME
        then
            PID=`cat $PIDFILE`
            start-stop-daemon --quiet --stop --signal INT --user $USERNAME --exec $DAEMON --pidfile $PIDFILE --name driver
            # Now we wait for it to die
            while kill -0 $PID 2>/dev/null; do sleep 1; done
            rm -f $PIDFILE
            echo "."
        else
            echo " not running.";
        fi
        echo -n "Stopping MUD Server: Bloodlines, addr_server"
        if start-stop-daemon --quiet --stop --signal 0 --pidfile $PIDFILE --name driver --user $USERNAME
        then
            APID=`cat $APIDFILE`
            start-stop-daemon --quiet --stop --signal INT --user $USERNAME --exec $ADAEMON --pidfile $APIDFILE --name addr_server
            # Now we wait for it to die
            while kill -0 $APID 2>/dev/null; do sleep 1; done
            rm -f $APIDFILE
            echo "."
        else
            echo " not running.";
        fi
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    status)
        if [ ! -f $PIDFILE ]; then
            echo "No PID file, game may be down"
            ps auxww | grep addr_server | grep -v grep
            ps auxww | grep driver | grep -v grep
        else
            ps auxww | grep `cat $APIDFILE` | grep -v grep
            ps auxww | grep `cat $PIDFILE` | grep -v grep
        fi
        ;;
    *)
        echo "Usage: /etc/init.d/bloodlines {start|stop|restart|status}"
        exit 1
esac

exit 0

Code: (start.addr_server) [Select]
#!/bin/bash

USERNAME=bloodlines
APORT=4990
MUDDIR=/home/bloodlines
PATH=$MUDDIR/bin:/bin:/usr/bin:/sbin:/usr/sbin
CONFIG=$MUDDIR/bin/mudos.cfg
STARTDIR=$MUDDIR/bin
LOGFILE=`/bin/date "+$MUDDIR/bin/driver_log.%y%m%d-%H%M%S"`

DAEMON=$MUDDIR/bin/driver
PIDFILE=$MUDDIR/bin/driver.pid
SCRIPT=$MUDDIR/bin/start_driver

ADAEMON=$MUDDIR/bin/addr_server
APIDFILE=$MUDDIR/bin/addr_server.pid
ASCRIPT=$MUDDIR/bin/start_addr_server

test -x $DAEMON || exit 0
test -x $ADAEMON || exit 0

echo -n "    Starting addr_server..."
if start-stop-daemon --quiet --stop --signal 0 --pidfile $APIDFILE --name addr_server --user $username
then
    echo " already running!"
    exit 0
else
    /home/bloodlines/bin/addr_server $APORT >/dev/null 2>&1 &
    ASTAT=$?
    echo $! >/home/bloodlines/bin/addr_server.pid
    echo " done."
    exit $ASTAT
fi

Code: (start.driver) [Select]
#!/bin/bash

USERNAME=bloodlines
APORT=4990
MUDDIR=/home/bloodlines
PATH=$MUDDIR/bin:/bin:/usr/bin:/sbin:/usr/sbin
CONFIG=$MUDDIR/bin/mudos.cfg
STARTDIR=$MUDDIR/bin
LOGFILE=`/bin/date "+$MUDDIR/bin/driver_log.%y%m%d-%H%M%S"`

DAEMON=$MUDDIR/bin/driver
PIDFILE=$MUDDIR/bin/driver.pid
SCRIPT=$MUDDIR/bin/start_driver

ADAEMON=$MUDDIR/bin/addr_server
APIDFILE=$MUDDIR/bin/addr_server.pid
ASCRIPT=$MUDDIR/bin/start_addr_server

test -x $DAEMON || exit 0
test -x $ADAEMON || exit 0

echo -n "    Starting driver..."
if start-stop-daemon --quiet --stop --signal 0 --pidfile $APIDFILE --name addr_server --user $username
then
    if start-stop-daemon --quiet --stop --signal 0 --pidfile $PIDFILE --name driver --user $username
    then
        echo " already running!"
        exit 0
    else
        /home/bloodlines/bin/driver $CONFIG >$LOGFILE 2>&1 &
        DSTAT=$?
        echo $! >/home/bloodlines/bin/driver.pid
        echo " done."
        exit $DSTAT
    fi
else
    echo " addr_server not running!"
    exit 0
fi

Offline mactorg

  • Acquaintance
  • *
  • Posts: 30
    • View Profile
    • DarkeMUD
Re: /etc/init.d scripts
« Reply #1 on: August 24, 2015, 06:25:34 PM »
Nice work quixadhal!

I will test this out on my test mud.  I control the server and it is in a co-location with generator backup so I don't often have to worry about reboots or random shutdowns but this is a nice thing to have if I am away and not able to get to the system if someone reboots it.

Again thanks!
MacTORG
to live is to learn, to die is to graduate.

http://www.darkemud.com

Offline quixadhal

  • BFF
  • ***
  • Posts: 618
    • View Profile
    • A Waste of Time
Re: /etc/init.d scripts
« Reply #2 on: August 26, 2015, 01:09:40 PM »
Ironically, we had a power outage for a half hour the other day, so I did get to tweak things a bit. :)

There were a couple of typos from cut/pasting, and as I expected I would, I got rid of the external scripts and just merged their function into the main one.

Enjoy!
Code: (bash) [Select]
#!/bin/sh
#
### BEGIN INIT INFO
# Provides:          bloodlines
# Required-Start:    $network $local_fs $remote_fs $syslog $named $time
# Required-Stop:     $network $local_fs $remote_fs $syslog $named $time
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: init-Script for FluffOS MUD driver
### END INIT INFO
#

USERNAME=bloodlines
APORT=4990
MUDDIR=/home/bloodlines
PATH=$MUDDIR/bin:/bin:/usr/bin:/sbin:/usr/sbin
CONFIG=$MUDDIR/bin/mudos.cfg
STARTDIR=$MUDDIR/bin
LOGFILE=`/bin/date "+$MUDDIR/bin/driver_log.%y%m%d-%H%M%S"`

DAEMON=$MUDDIR/bin/driver
PIDFILE=$MUDDIR/bin/driver.pid

ADAEMON=$MUDDIR/bin/addr_server
APIDFILE=$MUDDIR/bin/addr_server.pid

test -x $DAEMON || exit 0
test -x $ADAEMON || exit 0

case "$1" in
    start)
        echo "Starting MUD Server: Bloodlines"
        if start-stop-daemon --quiet --stop --signal 0 --pidfile $APIDFILE --name addr_server --user $USERNAME
        then
            APID=`cat $APIDFILE`
            echo "    addr_server [$APID] running."
        else
            echo -n "    Starting addr_server..."
            $ADAEMON $APORT >/dev/null 2>&1 &
            echo $! >$APIDFILE
            echo " done."
        fi
        #
        if start-stop-daemon --quiet --stop --signal 0 --pidfile $PIDFILE --name driver --user $USERNAME
        then
            PID=`cat $PIDFILE`
            echo "    driver [$PID] running."
        else
            echo -n "    Starting driver..."
            sleep 2
            $DAEMON $CONFIG >$LOGFILE 2>&1 &
            echo $! >$PIDFILE
            echo " done."
        fi
        ;;
    stop)
        echo -n "Stopping MUD Server: Bloodlines, driver"
        if start-stop-daemon --quiet --stop --signal 0 --pidfile $PIDFILE --name driver --user $USERNAME
        then
            PID=`cat $PIDFILE`
            start-stop-daemon --quiet --stop --signal INT --user $USERNAME --exec $DAEMON --pidfile $PIDFILE --name driver
            # Now we wait for it to die
            while kill -0 $PID 2>/dev/null; do sleep 1; done
            rm -f $PIDFILE
            echo "."
        else
            echo " not running.";
        fi
        echo -n "Stopping MUD Server: Bloodlines, addr_server"
        if start-stop-daemon --quiet --stop --signal 0 --pidfile $APIDFILE --name addr_server --user $USERNAME
        then
            APID=`cat $APIDFILE`
            start-stop-daemon --quiet --stop --signal INT --user $USERNAME --exec $ADAEMON --pidfile $APIDFILE --name addr_server
            # Now we wait for it to die
            while kill -0 $APID 2>/dev/null; do sleep 1; done
            rm -f $APIDFILE
            echo "."
        else
            echo " not running.";
        fi
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    status)
        if [ ! -f $PIDFILE ]; then
            echo "No PID file, game may be down"
            ps auxww | grep addr_server | grep -v grep
            ps auxww | grep driver | grep -v grep
        else
            ps auxww | grep `cat $APIDFILE` | grep -v grep
            ps auxww | grep `cat $PIDFILE` | grep -v grep
        fi
        ;;
    *)
        echo "Usage: /etc/init.d/bloodlines {start|stop|restart|status}"
        exit 1
esac

exit 0