Author Topic: Better Unix start script...  (Read 2270 times)

Offline Raudhrskal

  • BFF
  • ***
  • Posts: 214
  • The MUD community needs YOUR help!
    • View Profile
Better Unix start script...
« on: May 22, 2007, 01:59:31 am »
Well, I wanted to contribute this little thing that I'm using since 2005.
STILL 100% (d)(a)sh compatible, of course works also with bash and zsh's sh emulation.
Allow to conditionally exit the loop.

If you add somthing like "exec 2>&1 > /some/file" at the beginning, it should redirect all output to file, but I didn't tested that.

Code: [Select]
#!/bin/sh
#For ability to shutdown the mud, change line in /secure/cmds/admins/shutdown.c
#from:          call_out( (: shutdown :), 1);
#to:            call_out( (: shutdown :), 1, -1);
#and, if you want (afaik void ShutDown() isn't called anywhere but...)
#from           shutdown();
#to             shutdown(-1);
#and after updating command file and /daemon/command end will reboot (as usual)
#and shutdown will shutdown. If you want to use the 'shutdown' command
#for rebooting, you can add some parameter check, or simply copy shutdown.c
#to e.g. haltmud.c and change the call_out there.

#NOTE: To interrupt the while loop in script you may need to hold Ctrl-C for a moment
#      (first MudOS will be killed, and next the script - if you'll be still holding the key)
#      This will leave addr_server running in the background
#      just do     killall addr_server      or       kill `pidof addr_server`

# Specify your mud dir here.
MUDHOME="/opt/ds";

#Original author said:
# It has been reported that non-US users have problems
# with some .o files because their systems default to
# commas for decimal notation and not periods. The
# following language exports are for the benefit of
# people who run into this problem.

#LANG=en_US;
#LANGUAGE=en_US;
#LC_ALL=en_US;

#That's true. I had this problem too. But it should be
LANG=C;
LANGUAGE=C;
LC_ALL=C;
#C is the 'standard' locale. Right, the 'en_US' will still work,
#because if your system don't have specfic locale data for en_US
#it will automatically fall back to C. But what if system HAS
#some weird locale data for en_US? (some older HP-UX, for example)

export LANG LANGUAGE LC_ALL MUDHOME;

umask 007;

ulimit -n 1024;

echo "$0: `date`: Job starting...";

if [ $MUDHOME ] && [ -x $MUDHOME/bin/addr_server ];
        then #Run it in background
echo "$0: `date`: Running addr_server...";
        $MUDHOME/bin/addr_server 8099 & #change port if you need to - 8099 is in 2.3a1, older used 9999
addrpid=$!; #Catch the process ID
echo "$0: `date`: addr_server PID is" $addrpid;
fi

count=1; #boot count
retv=0;  #./driver return value

echo "$0: `date`: Entering loop.";

#Choose one option:
while [ $retv = "0" ]; do #continue ONLY if the driver exited gracefully - not by signal, not by efun shutdown(-1)
#while [ $retv != "255" ]; do #continue ALWAYS, even if the driver exited by signal, halt ONLY after efun shutdown(-1)

    echo "$0: `date`: Boot count:" $count;

    if [ $MUDHOME ] && [ -x $MUDHOME/bin/driver ]  && [ -f $MUDHOME/bin/mudos.cfg ];
    then
    $MUDHOME/bin/driver $MUDHOME/bin/mudos.cfg;
    retv=$?; #catch the return value
    echo "$0: `date`: Driver exit. Code" $retv;
    elif [ -x ./driver ]  && [ -f ./mudos.cfg ];
    then
    ./driver ./mudos.cfg;
    retv=$?; #catch the return value
    echo "$0: `date`: Driver exit. Code" $retv;
    else
    retv=255; #exit
    echo "$0: `date`: Can't find driver and/or mudos.cfg!";
    break;
    fi;
    count=$(($count+1));
    sleep 5; #this won't hurt, and sometimes even helps
done

echo "$0: `date`: Exiting loop.";

if [ $addrpid ]; #We started it?
then
echo "$0: `date`: Killing addr_server...";
kill $addrpid;
fi

echo "$0: `date`: End of job.";
I think, therefore i may be wrong.
Please note that if you met a Raudhrskal in a place that's not related to muds, it wasn't me. *sigh*... back when I started there was zero hits on google for that name...

Offline Raudhrskal

  • BFF
  • ***
  • Posts: 214
  • The MUD community needs YOUR help!
    • View Profile
Re: Better Unix start script...
« Reply #1 on: September 26, 2008, 12:20:01 pm »
A little updated version comes.
Still 100% 4.4BSD /bin/sh compatible, no bash required.
(Not tested on solaris, tho.)

Code: [Select]
#!/bin/sh
# Updated by Raudhrskal, 10.2006-01.2007, 07.2008

#For ability to shutdown the mud, change line in /secure/cmds/admins/shutdown.c
#from:          call_out( (: shutdown :), 1);
#to:            call_out( (: shutdown :), 1, -1);
#and, if you want (afaik void ShutDown() isn't called anywhere but...)
#from           shutdown();
#to             shutdown(-1);
#then after updating that file and /daemon/command 'end' will reboot (as before)
#and shutdown will shutdown. If you want to use the 'shutdown' command
#for rebooting, you can add some parameter check, or simply copy shutdown.c
#to e.g. haltmud.c and change the call_out there.

# Specify your mud dir here.
MUDHOME="/opt/mud/ds";

#This shouldn't be required now, but won't harm. Still useful for old versions.
LANG=C;
LANGUAGE=C;
LC_ALL=C;

export LANG LANGUAGE LC_ALL MUDHOME;

umask 007;

ulimit -n 1024;

#Handle Ctrl-C & friends somewhat gracefully. Allow the script to continue and kill addr_server.
#Note that these are signals the _shell_ receives, not the ones received by the driver.
#And handling shell's SIGSEGV in a shell script is a _poor_ idea.
trap 'echo "$0: `date`: SIGHUP received, exiting...";' HUP #1
trap 'echo "$0: `date`: SIGINT received, exiting...";' INT #2
trap 'echo "$0: `date`: SIGQUIT received, exiting...";' QUIT #3
trap 'echo "$0: `date`: SIGPIPE received, exiting...";' PIPE #13

echo "$0: `date`: Job starting...";

if [ $MUDHOME ] && [ -x $MUDHOME/bin/addr_server ];
        then #Run it in background
echo "$0: `date`: Running addr_server...";
        $MUDHOME/bin/addr_server 8099 & #change port if you need to - 8099 is since 2.3a1, older used 9999
addrpid=$!; #Catch the process ID
echo "$0: `date`: addr_server PID is" $addrpid;
fi

count=1; #boot count
retv=0;  #./driver return value

echo "$0: `date`: Entering loop.";

#Choose one option:
while [ $retv = "0" ]; do #continue ONLY if the driver exited gracefully - not by signal, not by efun shutdown(-1)
#while [ $retv != "255" ]; do #continue ALWAYS, even if the driver exited by signal, halt ONLY after efun shutdown(-1)

    echo "$0: `date`: Boot count:" $count;

    if [ $MUDHOME ] && [ -x $MUDHOME/bin/driver ]  && [ -f $MUDHOME/bin/mudos.cfg ];
    then
    $MUDHOME/bin/driver $MUDHOME/bin/mudos.cfg;
    retv=$?; #catch the return value
    echo "$0: `date`: Driver exit. Code" $retv;
    elif [ -x ./driver ]  && [ -f ./mudos.cfg ];
    then
    ./driver ./mudos.cfg;
    retv=$?; #catch the return value
    echo "$0: `date`: Driver exit. Code" $retv;
    else
    retv=255; #exit
    echo "$0: `date`: Can't find driver and/or mudos.cfg!";
    break;
    fi;
    count=$(($count+1));
    sleep 5; #this won't hurt, and sometimes even helps
done

echo "$0: `date`: Exiting loop.";

if [ $addrpid ]; #We started it?
then
    echo "$0: `date`: Killing addr_server...";
    kill $addrpid;
fi

echo "$0: `date`: End of job.";

Share & enjoy,
'Skal
I think, therefore i may be wrong.
Please note that if you met a Raudhrskal in a place that's not related to muds, it wasn't me. *sigh*... back when I started there was zero hits on google for that name...