PDA

View Full Version : My server autostart script


asktoby
17-03-2004, 12:36
Hi

I wanted the teamspeak server to always be running on my PC.

I've put this short script in my ~/.bash_profile file to check if the server is already running, and if it is not, to start it.

I find it useful because I sometimes reboot linux, and often log into linux remotely. Without checking if the server was already running, I would get various issues.

This is my first attempt at shell scripting, it seems to work well, but I'd appreciate any comments!


cat ~/Documents/downloads/teamspeak/tss2_rc2/tsserver2.pid
if test $? -eq 1
then
cd ~/Documents/downloads/teamspeak/tss2_rc2
./server_linux -PID=tsserver2.pid
echo tsserver2.pid not found, tsserver started!
else
echo tsserver.pid found. tsserver is assumed to be running.
fi
cd


Obviously, replace ~/Documents/downloads... with the path to your own teamspeak install directory.

asktoby
19-03-2004, 14:20
A modified, cleaner, nicer version :)


# == Teamspeak autostarter script =====
if [ -f ~/Documents/downloads/teamspeak/tss2_rc2/tsserver2.pid ]; then
echo "tsserver.pid found. Teamspeak is assumed to be running."
else
cd ~/Documents/downloads/teamspeak/tss2_rc2
./server_linux -PID=tsserver2.pid
cd
fi
# == End of Teamspeak script ==========

asktoby
28-06-2004, 14:12
I should point out that this doesn't seem to actually work :D

It seems I can't rely upon the existance of tsserver2.pid to show if teamspeak server is running.

Anyone got a better idea?

Bruisah
03-07-2004, 11:43
I should point out that this doesn't seem to actually work :D

It seems I can't rely upon the existance of tsserver2.pid to show if teamspeak server is running.

Anyone got a better idea?

Try the following (though I've not tested it thouroghly! ;) )

# == Teamspeak autostarter script =====

# Quietly check for ts process running
ps -ef | grep -v grep | grep -q server_linux

if [[ $? -eq 0 ]] # Note double [[ ]] are important!
then
echo "tsserver.pid found. Teamspeak is running."
exit 0 # We can exit the script nowelse
# Process is *not* running
echo "Teamspeak server not running - starting now..."

# Record current DIR
pwd=$PWD

cd ~/Documents/downloads/teamspeak/tss2_rc2

# Note, I assume you have the default start script that
# should have been installed:
./teamspeak2-server_startscript start

# This is better than the line below!
# ./server_linux -PID=tsserver2.pid

cd $pwd # Change back to our previous dirfi

# Check to see if TS has started up
ps -ef | grep -v grep | grep -q server_linux

if [[ $? -eq 0 ]]
then
echo "tsserver.pid found. Startup successful."else
echo "Teamspeak still not started - please check server.log"
fi

# == End of Teamspeak script ==========

FYI:

The command: ps -ef | grep -v grep | grep -q server_linux

Does this:

ps -ef -> list all running processes on the CPU

| grep -v grep -> pipe the results through grep and remove any entry that includes the pattern 'grep'

| grep -q server_linux -> pipe the results through grep *silently* searching for the pattern 'server_linux'

Try it on the command line to see it working! Normaly, the result of grep will be printed on stdio, but the -q flag tells it to go silent instead.

However, the reults of grep will either be success (pattern match) or fail, which will set the exit code of grep accordingly (1 or 0 respectively).

We can check the exit code by looking at th $? variable (we check to see if it is 0 or not).

I hope this all makes sense! If not, drop me a line...

Happy scripting/TSpeaking!

Bruisah

Leyte
10-07-2004, 22:49
Very nice, I ran it from the prompt and it did what it was supposed to. :D Now being a non-techie in Linux, what is the best way to run a script like this? I am using Redhat 9.

Thanks,

Gary

asktoby
14-07-2004, 11:41
Cool modifications Brusiah, thanks. Now I'm trying to work out how to stop the
pwd = $PWD
line from echoing. It's messy! I've tried various things like -q --quiet, stty -echo, etc, but can't get it to be quiet. There's no manpage for "="!
Any clues?

Oh, and Leyte: I call this script from my ~/.bash_profile script so that it is run wheneever I log in.