massasster

Ubuntu - Server restarts + server reboot - the easy way

8 posts in this topic

some quick fixes for rebooting and restarted downed servers under linux

 

If you are running multiple instances of arma3server this will require that you edit the name of the exec file -   IE

arma3server2

arma3server3    etc

you CANNOT leave one of them named arma3server and just rename the next one arma3server2...  it will assume all other servers fall under arma3server and never reboot the other servers if they drop.

 

 

Set a cronjob for server restarts -  

 

https://help.ubuntu.com/community/CronHowto

23r4t3s.jpg

 

Setting start up on server boot

 

/etc     file name is   rc.local

http://ubuntuforums.org/showthread.php?t=1709280

rwi1wp.jpg

(NOTE: the last entry into my Rc.local is a monitor script)

 

 

 

and the monitor script

 

#!/bin/bash
#check if server is running by MassAsster

while true; do

if pgrep arma3server1 >/dev/null 2>&1
  then
     echo "arma3server running"
  else
     echo "arma3server not running sending restart"
cd /home/steam/steamcmd/arma3/
./arma3server1 -maxmem=2047 -config=server.cfg -port="2302"&

fi

if pgrep arma3server2 >/dev/null 2>&1
  then
     echo "arma3server2 running"
  else
     echo "arma3server2 not running sending restart"
cd /home/steam/steamcmd/arma3server2/
./arma3server2 -maxmem=2047 -config=server7.cfg -port=2315 -autoinit -mod="@Exile;@ryanzombies;@mas;@masvehicle;@tryk;@AllInArmaTerrainPackLite;" -servermod="@ExileServer;@A3XAI;@infistar;@AllInArmaTerrainPackLite;@mas;@masvehicle"&


fi


if pgrep arma3server3 >/dev/null 2>&1
  then
     echo "arma3server3 running"
  else
     echo "arma3server3 not running sending restart"
cd /home/steam/steamcmd/arma3server3/
./arma3server3 -maxmem=2047 -config=server8.cfg -port=2330 -autoinit -mod="@Exile;@ryanzombies;@mas;@masvehicle;@tryk;@AllInArmaTerrainPackLite" -servermod="@ExileServer;@A3XAI;@infistar;@AllInArmaTerrainPackLite;@mas;@masvehicle"&

fi

echo "sleeping 2 minutes"
sleep 2m
done

 

 

The echo was simply for debugging -  as this runs without the console being open.

 

 

do note that all this is rather redundant as you really can simply just run the monitor script on start up, rather then issuing the server startup commands -  but, I like to be sure things fired off so I use redundant methods 

Edited by massasster
  • Like 2

Share this post


Link to post
Share on other sites

Someone needed a more detailed explanation of how to create the start up - so I'll leave this here incase anyone else needs it.

(ALSO: I bugged Chris (Infistar) to add #shutdown to the tool, so manual cronjob of 6 hour restart is no longer required for me. I could never get the default shutdown built into exile to function so I was forced to use cronjob to do it.)

you are basically going to make a script inside a folder,  doesn't matter where you create the script 

 

i use   VIM

 

for this purpose I'm going to pretend that my install directory is at   /home/steam/arma3server

 

 

so i would  cd /home/steam/arma3server

 

vim restartscript.sh

 

(press in the insert key on the keyboard to begin to type)

----------------------------------------------------------------------------------------------

#!/bin/bash

#check if arma server is running

         COUNTER=0
         while [  $COUNTER -lt 10 ]; do

if pgrep arma3server >/dev/null 2>&1
  then
     echo "arma3server running"
  else
     echo "arma3server not running"

cd /home/steam/arma3server

./arma3server -maxmem=2047 -config=server7.cfg -port=2302 -autoinit -mod="@Exile;@AllInArmaTerrainPackLite;" -servermod="@ExileServer;@A3XAI;@infistar;@AllInArmaTerrainPackLite"&


fi


echo "sleeping 2 minutes"
sleep 2m

done

 

 

-------------------------------------------------------------------------------------------------------

at that point press escape -  and save the file using command    :w

 

will have have to chmod the file     chmod +x restartscript.sh

 

then from there you can just execute your server restart script,  the "&" keeps the server (or script, or whatever) going after you close out the terminal window

 

 

cd /home/steam/arma3server

./restartscript.sh &

 

 

 

this will continue to run until you reboot the linux box -  should be no need to restart the box, but you might want to look at my example of startup's in the event something does happen on how to start this script on server reboot

 

Edited by massasster

Share this post


Link to post
Share on other sites
Advertisement

if you run your debug in screen it should play nicer. I have an example somewhere on my drives, let me see if I can find it.



Obviously, all you really need is
screen -dmS
screen -p 0 -S -X stuff "bash ./restartscript.sh"




Also, you should just be restarting the arma3server executable (with kill), I don't think there is any need to reboot the physical computer.

Yeah, here's an example for my mining cluster manager,

#!/bin/bash
cd /root/master
screen -dmS mojo
sleep 5
screen -p 0 -S mojo -X stuff "bash
./mojo
" &
sleep 60
export DISPLAY=':1'
xrandr -s 640x480
xterm -geometry =80x24+10+10 -fn 8x13 -j -fg white -bg black -e screen -x mojo
Edited by wasti
  • Like 1

Share this post


Link to post
Share on other sites

Hello, I am having some problems running a modified version of your script. I am using CronTab to run the restartscript.sh instead of having the sleep command, since I don't want it running in the background forever, as well as I don't want to make a separate screen for just this.

My crontab looks like this:

Spoiler

*/2 * * * * sh /[arma3directory]/restartscript.sh

And my restartscript.sh looks like this:

Spoiler

if pgrep arma3server >/dev/null 2>&1
then
    echo "Exile running"
else
    echo "Exile not running sending restart"
    cd /home/steam/arma3
    screen -dmS exile
    sleep 5
    screen -p 0 -S exile -X stuff "bash ./home/steam/arma3/start-exile"

I am running the start exile script which contains the startup stuff.

I am getting the error: ./restartscript.sh: line 10: syntax error: unexpected end of file

Obviously a shell error, but how to fix?

Thanks,
Freece

Edited by Freece

Share this post


Link to post
Share on other sites

my guess is you created your script inside windows and uploaded it to your linux box

 

you have two options

you can use a windows to unix conversion tool  ,  like http://www.tutorialspoint.com/unix_commands/dos2unix.htm ; http://stackoverflow.com/questions/82726/convert-dos-line-endings-to-linux-line-endings-in-vim

 

OR: you can create a new .sh in linux properly through SSH

 

 

 

--------------edit 

 

also your formatting is wrong

 

 

#!/bin/bash
#check if server is running

while true; do
if pgrep arma3server >/dev/null 2>&1
then
    echo "Exile running"
else
    echo "Exile not running sending restart"
    cd /home/steam/arma3
    screen -dmS exile
    sleep 5
    screen -p 0 -S exile -X stuff "bash ./home/steam/arma3/start-exile"

fi


echo "sleeping 2 minutes"
sleep 2m

done

 

 

Wasti's idea of running things in screen is fine , but not really necessary for the script to function. Really depends on how much you will be accessing the server and if the script "hint" messages will be in the way, and if that's the case, you can simply "mute" the script by removing all the echo commands.

Edited by massasster

Share this post


Link to post
Share on other sites

Here is what I use.

/etc/init.d/exile

#!/bin/sh
### BEGIN INIT INFO
# Provides:          <NAME>
# Required-Start:    $local_fs $network $named $time $syslog
# Required-Stop:     $local_fs $network $named $time $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Description:       <DESCRIPTION>
### END INIT INFO

SCRIPT=/home/steam/steamcmd/arma3/scripts/restarter.sh
RUNAS=steam

PIDFILE=/var/run/exile.pid
LOGFILE=/home/steam/steamcmd/arma3/server.log

start() {
  if [ -f /var/run/$PIDNAME ] && kill -0 $(cat /var/run/$PIDNAME); then
    echo 'Service already running' >&2
    return 1
  fi
  echo 'Starting service…' >&2
  local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
  su -c "$CMD" $RUNAS > "$PIDFILE"
  echo 'Service started' >&2
}

stop() {
  if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
    echo 'Service not running' >&2
    return 1
  fi
  echo 'Stopping service…' >&2
  kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
  echo 'Service stopped' >&2
}

uninstall() {
  echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
  local SURE
  read SURE
  if [ "$SURE" = "yes" ]; then
    stop
    rm -f "$PIDFILE"
    echo "Notice: log file is not be removed: '$LOGFILE'" >&2
    update-rc.d -f <NAME> remove
    rm -fv "$0"
  fi
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  uninstall)
    uninstall
    ;;
  retart)
    stop
    start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|uninstall}"
esac

/home/steam/steamcmd/aram3/scripts/restarter.sh

#!/bin/bash

# ARMA3/Exile location
ARMA_DIR=/home/steam/steamcmd/arma3/

# Track running state
running=true

# Use signal to notify server of shutdown
_shutdown() {
    echo "Shutting down server ($child)!"

    running=false # Mark as not running, loop will exit
    
    # Sent SIGINT to server, similar to #shutdown
    kill -s SIGINT "$child" 2>/dev/null
}

trap _shutdown SIGINT SIGTERM

while $running; do
    echo "Starting Exile Server"

    # Start ARMA3/Exile server
    cd $ARMA_DIR  
    ./arma3server -config=config.cfg -cfg=basic.cfg -mod=@exile -servermod=@exileserver\;@infistar_exile\;@a3_dms -autoinit -malloc=tbbmalloc -nologs -high &

    child=$! # Get child PID

    echo "Server started ($child)"
    wait "$child" # Wait for child to exit
    
    echo "Exile server stopped or crashed"
    sleep 1 # Wait a second before restarting
done

exit 0


 

Share this post


Link to post
Share on other sites
On 1/15/2016 at 9:57 PM, ENETDOWN said:

Here is what I use.

Spoiler

 

/etc/init.d/exile



#!/bin/sh
### BEGIN INIT INFO
# Provides:          <NAME>
# Required-Start:    $local_fs $network $named $time $syslog
# Required-Stop:     $local_fs $network $named $time $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Description:       <DESCRIPTION>
### END INIT INFO

SCRIPT=/home/steam/steamcmd/arma3/scripts/restarter.sh
RUNAS=steam

PIDFILE=/var/run/exile.pid
LOGFILE=/home/steam/steamcmd/arma3/server.log

start() {
  if [ -f /var/run/$PIDNAME ] && kill -0 $(cat /var/run/$PIDNAME); then
    echo 'Service already running' >&2
    return 1
  fi
  echo 'Starting service…' >&2
  local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
  su -c "$CMD" $RUNAS > "$PIDFILE"
  echo 'Service started' >&2
}

stop() {
  if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
    echo 'Service not running' >&2
    return 1
  fi
  echo 'Stopping service…' >&2
  kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
  echo 'Service stopped' >&2
}

uninstall() {
  echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
  local SURE
  read SURE
  if [ "$SURE" = "yes" ]; then
    stop
    rm -f "$PIDFILE"
    echo "Notice: log file is not be removed: '$LOGFILE'" >&2
    update-rc.d -f <NAME> remove
    rm -fv "$0"
  fi
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  uninstall)
    uninstall
    ;;
  retart)
    stop
    start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|uninstall}"
esac

/home/steam/steamcmd/aram3/scripts/restarter.sh



#!/bin/bash

# ARMA3/Exile location
ARMA_DIR=/home/steam/steamcmd/arma3/

# Track running state
running=true

# Use signal to notify server of shutdown
_shutdown() {
    echo "Shutting down server ($child)!"

    running=false # Mark as not running, loop will exit
    
    # Sent SIGINT to server, similar to #shutdown
    kill -s SIGINT "$child" 2>/dev/null
}

trap _shutdown SIGINT SIGTERM

while $running; do
    echo "Starting Exile Server"

    # Start ARMA3/Exile server
    cd $ARMA_DIR  
    ./arma3server -config=config.cfg -cfg=basic.cfg -mod=@exile -servermod=@exileserver\;@infistar_exile\;@a3_dms -autoinit -malloc=tbbmalloc -nologs -high &

    child=$! # Get child PID

    echo "Server started ($child)"
    wait "$child" # Wait for child to exit
    
    echo "Exile server stopped or crashed"
    sleep 1 # Wait a second before restarting
done

exit 0

 

 

 

You my friend are pretty awesome, thank you for sharing this!

Kind Regards,
-b3ck

Edited by b3ck

Share this post


Link to post
Share on other sites
Advertisement

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.