Scripts:RestartScript

From BF2 Technical Information Wiki
Jump to navigation Jump to search

Server Restart Script (Linux Only)

The two scripts underneath are to be used in combination. These have as purpose to re-run the Battlefield 2 Dedicated Server when it crashes or quits.

startit.sh

This script initializes the servermonitor script in a screen. If you wish to run multiple server instances you need to change 'screenname' to a different name.

NOTE: Make sure to change /path/to/bf2 to your bf2 install dir!!!

#! /bin/sh

screen -dmS screenname /path/to/bf2/servermonitor

/path/to/bf2/servermonitor

This script constantly runs to check wheter the server still runs and restarts it when it doesnt run anymore.

NOTE: Make sure to change /path/to/bf2 to your bf2 install dir!!!

The only line needed to be changed will be 'exec=/path/to/bf2/start.sh' (line 4)

#! /bin/sh

#full path to the start script for the process you want to monitor
exec=/path/to/bf2/start.sh

#interval the script checks to see if the process is still running
interval=10

while :
do
pid=$(ps ax | grep bf2 | grep bin | grep -v grep | awk '{print $1}')
echo $pid
if [ -z "$pid" ]
then
date >> restartlog
echo $procName has stopped!!!! Starting $procName!!!! >> restartlog
echo " "
echo " "

$exec
fi

sleep $interval
done

Made By

These scripts where delivered to me by David Stevens and according to him they have been build by Rene Belloq.

--Mortis 13:05, 11 Jul 2005 (MDT)

Addendum

Just copying the scripts above did not work for me on an actual Linux dedicated server with SuSE 10.2 running.

You should now that i wanted to use them for our BF2 server running the AIX mod on it.

First: I used as screenname "AIXMOD"

#! /bin/sh

screen -dmS AIXMOD /path/to/bf2/servermonitor

But "servermonitor" did not work, because the command for calling BF2 with the Mod parameter "exec=/path/to/bf2/start.sh +modPath mods/aix" is not known by the bash. Try to use a line calling it the way the shell can handle it instead:

#! /bin/sh
 
#full path to the start script for the process you want to monitor
exec='./start.sh +modPath mods/aix +ignoreAsserts +fileMonitor 1'

#interval the script checks to see if the process is still running
interval=10

O.K., finally "servermonitor" started the BF2 server with the AIX mod. But then, the next "problem": After the BF2 server crashed, the "servermonitor" did just a loop, always writing into the "restartlog" that the server is restarting. Again and again, but the server did not come up again. A short "ps x | grep bf2" showed why:

11743 ?        S      0:00 sshd: bf2server@notty
11804 ?        S      0:00 sshd: bf2server@pts/1
11982 ?        Ss     0:00 SCREEN -dmS AIXMOD /home/bf2server/bf2/servermonitor
11983 pts/2    Ss+    0:00 /bin/sh /home/bf2server/bf2/servermonitor
11984 pts/2    Rl+    4:38 /home/bf2server/bf2/bin/amd-64/bf2 +modPath mods/aix
12328 pts/1    S+     0:00 grep bf2

The script finds way too many pids, if you install your server files e.g. into a directory called "bf2" (as we did). So we checked the default grep command of the script and got 2 pids:

bf2server@s15259444:~/bf2> ps x | grep bf2 | grep bin | grep -v grep | awk '{print $1}'
11983
11984
bf2server@s15259444:~/bf2>

One of the "servermonitor" itself (pid:11983) and one of the BF2 server running (pid:11984). In order to get just the "interesting" pid we changed the grep command towards:

bf2server@s15259444:~/bf2> ps x | grep bf2 | grep bin | grep -v grep | grep -v servermonitor | awk '{print $1}'
11984
bf2server@s15259444:~/bf2>

You see ^^, now we get only the pid of the "running" BF2 server, if the BF2 server crashes, that pid will be missing and the restart should work as intended. You should have got the principle how it works, change yours accordingly ;) Hope it helps.

I forgot: You can also change the original grep command "grep bf2" towards a call for the part "amd-64" or "ia-32", depending which you're using on your running AIX server:

bf2server@s15259444:~> ps x | grep amd-64 | grep bin | grep -v grep | awk '{print $1}'
11984
bf2server@s15259444:~>

--BlackBears1 10:36, 16 January 2008 (MST)

NARS (Not Another Restart Script)

This Script has been made to be used under a Linux Screen

Script

#!/bin/bash
#
# Press Ctrl+C to stop the restarting
echo "##########################"
echo "# Bf2 Server Starting #"
echo "##########################"
echo To stop the restarting press Ctrl+C when the server is being restarted
echo
trap 'echo; echo $SRV Server Restarter has been STOPPED!; exit 1' 2
C1=0
while true
do
C1=$((C1+=1))
cd /home/bf2srv/bf2
# Modify the line bellow to match your server execution command line
./start.sh
echo "Bf2 server restarted $C1 time(s)!"
sleep 10
done

Usage

First, modify the path to the BF2 server directory on the script. Also you can add some extra variables to the ./start.sh script (like modPath or others). Then save the script as, for example, server_start.sh and store it on the bf2 directory.

Do a:

chmod +x server_start.sh 

Then, open a new screen (just type screen, then enter), 'inside' the screen, go to the BF2 server directory and type:

./server_start.sh 

The server will start normally, then detach the screen by pressing 'CTRL + A' and then 'D', the results must be something like:

[detached] 

Yo can resume the screen and check the server console by doing a:

screen -r PID 

While PID is the process ID of the screen. If there are no other screen instanses running, you can resume this one using only screen -r

If there is other screens running, the screen -r command will tell you the PIDs of those screens.

Notes

This script will restart your server if it crashes or quit. Is very simple on the practice, dont be scared.

--DarkPepe 03:32, 14 May 2006 (MDT)