Server CTL Script

Need help seting up a server, or have a question on how to run one? This is the place.
Post Reply
User avatar
Gamma Ray
Private First Class
Private First Class
Posts: 22
Joined: Sun Aug 17, 2008 5:29 pm
Location: Nowhere

Server CTL Script

Post by Gamma Ray » Sat Aug 20, 2011 12:57 pm

I am currently working on a script for BZFS to work like apachectl. Is there any way of controlling the server from a separate process, i.e. not the one that launched it, without using loging in to bzadmin? I'm looking for a better force-shutdown method than

Code: Select all

killall bzfs
something with the same effect as crtl + c does on the running bzfs?

If not I'll just use bzadmin

User avatar
blast
General
General
Posts: 4736
Joined: Fri Mar 21, 2003 3:49 pm
Location: playing.cxx
Contact:

Re: Server CTL Script

Post by blast » Sat Aug 20, 2011 2:48 pm

You can use the -pidfile option of bzfs to write out the PID (Process ID) of the bzfs instance to a file of your choice. Then you can use that PID what you want to close down a specific server.
"In addition to knowing the secrets of the Universe, I can assure you that I am also quite potty trained." -Koenma (Yu Yu Hakusho)

Image

User avatar
Gamma Ray
Private First Class
Private First Class
Posts: 22
Joined: Sun Aug 17, 2008 5:29 pm
Location: Nowhere

Re: Server CTL Script

Post by Gamma Ray » Sat Aug 20, 2011 4:24 pm

blast wrote:You can use the -pidfile option of bzfs to write out the PID (Process ID) of the bzfs instance to a file of your choice. Then you can use that PID what you want to close down a specific server.
Thanks, but do you know how to start a background process in sh?

User avatar
SkillDude
Private First Class
Private First Class
Posts: 336
Joined: Sun Apr 01, 2007 4:50 pm
Location: United States

Re: Server CTL Script

Post by SkillDude » Sat Aug 20, 2011 5:54 pm

A process will be sent to the background by placing an ampersand (&) sign after the command. There are many tutorials and references online to find BASH and linux scripts that you can search for. No need to ask here.

User avatar
Gamma Ray
Private First Class
Private First Class
Posts: 22
Joined: Sun Aug 17, 2008 5:29 pm
Location: Nowhere

Re: Server CTL Script

Post by Gamma Ray » Sat Aug 20, 2011 10:02 pm

sigonasr2 wrote:A process will be sent to the background by placing an ampersand (&) sign after the command.
I already tried this and output is still in the same terminal. I want to be able to run my script, and close the window without it closing bzfs. I've already asked around a bunch of other forums and I still can't find an answer. Nohup redirects output to a file but I don't need the output at all! Also Nohup causes some strange output in the current window.
I want to launch bzfs in a seperate process that would be the same as if you double clicked it. BZFS should be left running like a regular unix daemon as a child of init.
Last edited by Gamma Ray on Mon Jun 18, 2012 11:41 pm, edited 1 time in total.

User avatar
SkillDude
Private First Class
Private First Class
Posts: 336
Joined: Sun Apr 01, 2007 4:50 pm
Location: United States

Re: Server CTL Script

Post by SkillDude » Sat Aug 20, 2011 11:05 pm

How about screen? Screen is a tool that lets you put something up, then it runs in a separate virtual "window" on your system, leaving it be while you take care of other things. I believe screen can accept an input file to execute when it is launched if that helps at all.

There are some good things to know about it. You would have to learn how to attach and detach from screens, and even the ability to create child windows inside one screen. It can all be done, you could private message me for more details on doing it this way.

User avatar
Gamma Ray
Private First Class
Private First Class
Posts: 22
Joined: Sun Aug 17, 2008 5:29 pm
Location: Nowhere

Re: Server CTL Script

Post by Gamma Ray » Sat Aug 20, 2011 11:19 pm

But I just want to run bzfs the same way httpd and ftpd and other server daemons run. How does apachectl work if not by doing that?

User avatar
joevano
General
General
Posts: 1863
Joined: Sat Jun 18, 2005 1:08 pm
Location: South Bend, Indiana, USA

Re: Server CTL Script

Post by joevano » Sat Aug 20, 2011 11:41 pm

Gamma Ray wrote:
sigonasr2 wrote:A process will be sent to the background by placing an ampersand (&) sign after the command.
I already tried this and it still in the same terminal. I want to have be able to run my script, and close the window without it closing bzfs. I've already asked around a bunch of other forums and I still can't find an answer. Nohup redirects out put to a file but I don't need the output at all! Also Nohup causes some strange output in the current window.
I want to launch bzfs in a seperate process that would be the same as if you double clicked it. BZFS should be left running like a regular unix daemon as a child of init.
Learn how to use nohup...
There is nothing worse than aggressive stupidity. -- Johann Wolfgang von Goethe
"How many legs does a dog have if you call his tail a leg? Four. Calling a tail a leg doesn't make it a leg." -- Abraham Lincoln

User avatar
Gamma Ray
Private First Class
Private First Class
Posts: 22
Joined: Sun Aug 17, 2008 5:29 pm
Location: Nowhere

Re: Server CTL Script

Post by Gamma Ray » Sun Aug 21, 2011 12:33 am

But Nohup still stops the script unless you run nohup bzfs & and even then it occasionally writes things like:

[1]- Exit 1 nohup bzfs
[2]+ Exit 1 nohup bzfs

I want bzfs running outside of the shell

Even exec & bzfs & still dumps output to the shell

what does:
[2]+ Exit 1 "/usr/X11/bin/glxgears"
mean?

User avatar
blast
General
General
Posts: 4736
Joined: Fri Mar 21, 2003 3:49 pm
Location: playing.cxx
Contact:

Re: Server CTL Script

Post by blast » Sun Aug 21, 2011 12:44 am

This is the script I use to run my servers currently. It's far from perfect though:

run.sh

Code: Select all

#!/bin/bash
(
while [ ! -f stop ]
        do
        $HOME/2.4/bin/bzfs -conf config -pidfile pid >> log 2>&1
        rm pid
        sleep 10
done
rm stop
) &
And then to stop I use this (again, far from perfect):

stop.sh

Code: Select all

#!/bin/bash

touch stop

if [ -e pid ]
then
  kill `cat pid`
  sleep 5
fi

if [ -e pid ]
then
  kill -9 `cat pid`
  rm pid
fi
I have my servers run from their own account, and each server is in their own directory. I have a 'shared' directory that contains these scripts as well as my ban/group/etc files, and then each server's directory has symlinked copies of the scripts. So then I can run the 'stop.sh' script in that server's folder and it will shut that one down. Both scripts assume they are being run from the server's folder.
"In addition to knowing the secrets of the Universe, I can assure you that I am also quite potty trained." -Koenma (Yu Yu Hakusho)

Image

User avatar
Gamma Ray
Private First Class
Private First Class
Posts: 22
Joined: Sun Aug 17, 2008 5:29 pm
Location: Nowhere

Re: Server CTL Script

Post by Gamma Ray » Sun Aug 21, 2011 12:49 am

Thank you Blast I'm still trying to figure that out but here's what I have so far:

Code: Select all

#!/bin/sh
#
# if you like symbolic links:
# BZFS_EXEC_L '/Library/BZFlagServer/bin/'
BZFS_EXEC_L='/Applications/BZFlag-2.4.0.app/Contents/MacOS/'
BZFS_FILE_L='/Library/BZFlagServer/'
#bold=`tput bold`
#nobold=`tput rmso`

start(){
exec "$BZFS_EXEC_L"bzfs #-conf "$BZFS_FILE_L"conf/bzfs.conf $2
}
force_stop(){
echo 'It is recommended to shutdown the server with the stop command. Terminating bzfs this way can result in lost data.'
echo 'Are you sure you want to force the server to terminate? [y/n]:'
read -n1 -s tmp
if [ $tmp = 'y' ];then
echo 'sending SIGTERM ...'
tmp=`killall bzfs`
if [ $tmp = 'No matching processes' ];then
echo 'BZFlag Server is not running or could not be stopped.'
else
echo 'BZFlag Server Terminated' 
fi
else
echo 'Aborted'
fi
}
help(){
echo '    start        : start the BZFlag Server'
echo '    stop         : stop the BZFlag Server'
echo '    restart      : stops and starts the BZFlag Server'
echo '    term         : terminate running BZFlag Server'
echo '    help         : displays this help'
echo ''
}
fall(){
echo 'BZFlag Server Control Interface for:'
exec "$BZFS_EXEC_L"bzfs -v
}
case $1 in
'start')start;;
'stop')shutdwn;;
'restart')shutdwn;start;;
'term')force_stop;;
'help')help;;
*)fall;;
esac
It's based a bit off of a server CTL script

User avatar
blast
General
General
Posts: 4736
Joined: Fri Mar 21, 2003 3:49 pm
Location: playing.cxx
Contact:

Re: Server CTL Script

Post by blast » Sun Aug 21, 2011 1:32 pm

Also note that I do start my servers within a screen instance and the original shell that starts them remains running. I haven't tried launching them straight from a shell to see if they keep running after I close it.
"In addition to knowing the secrets of the Universe, I can assure you that I am also quite potty trained." -Koenma (Yu Yu Hakusho)

Image

User avatar
SkillDude
Private First Class
Private First Class
Posts: 336
Joined: Sun Apr 01, 2007 4:50 pm
Location: United States

Re: Server CTL Script

Post by SkillDude » Sun Aug 21, 2011 9:44 pm

In general any process that runs from the shell directly will terminate if that terminal connection is closed. Running it in screen or putting it to a background process will allow it to continue running without problems. Although screen is your best option for still allowing input through the interface at this point.

User avatar
ahs3
Private First Class
Private First Class
Posts: 315
Joined: Sun Mar 04, 2007 8:33 pm
Location: Press '/' to search
Contact:

Re: Server CTL Script

Post by ahs3 » Mon Aug 22, 2011 12:15 am

sigonasr2 wrote:In general any process that runs from the shell directly will terminate if that terminal connection is closed. Running it in screen or putting it to a background process will allow it to continue running without problems. Although screen is your best option for still allowing input through the interface at this point.

Actually you can run from the terminal and leave it running. There is a order to things. Run the server as you would normally do. 'bzfs -conf coolmap.conf >> coolmap.log &' or run any script with the following '&'. Then do Ctrl+C, and the important part, you must exit the terminal with the command 'exit' instead of closing the terminal window. The same could be done with any script or action.

User avatar
SkillDude
Private First Class
Private First Class
Posts: 336
Joined: Sun Apr 01, 2007 4:50 pm
Location: United States

Re: Server CTL Script

Post by SkillDude » Mon Aug 22, 2011 2:23 am

Ah, good point to bring up ahs3. This is why my "keepalive" scripts follow this general sequence:

Code: Select all

bzfs
./keepalive.sh &
exit

User avatar
joevano
General
General
Posts: 1863
Joined: Sat Jun 18, 2005 1:08 pm
Location: South Bend, Indiana, USA

Re: Server CTL Script

Post by joevano » Mon Aug 22, 2011 7:48 am

Not to throw a big wrinkle into this.... but how would you run multiple maps? Each requires it's own instance of bzfs, which is unlike apache sites.
There is nothing worse than aggressive stupidity. -- Johann Wolfgang von Goethe
"How many legs does a dog have if you call his tail a leg? Four. Calling a tail a leg doesn't make it a leg." -- Abraham Lincoln

User avatar
ahs3
Private First Class
Private First Class
Posts: 315
Joined: Sun Mar 04, 2007 8:33 pm
Location: Press '/' to search
Contact:

Re: Server CTL Script

Post by ahs3 » Mon Aug 22, 2011 10:30 am

joevano wrote:Not to throw a big wrinkle into this.... but how would you run multiple maps? Each requires it's own instance of bzfs, which is unlike apache sites.

The same way, just start as many instances of bzfs or any proc, following each command with Ctrl+C. Then use exit to close the terminal. Or write a simple script to start them all, with a simple sleep timer to ensure they have time to start.
$ bzfs -conf cool.conf &
Ctrl+c
$ bzfs -conf cool2.conf &
Ctrl+c
$ exit

User avatar
joevano
General
General
Posts: 1863
Joined: Sat Jun 18, 2005 1:08 pm
Location: South Bend, Indiana, USA

Re: Server CTL Script

Post by joevano » Mon Aug 22, 2011 12:32 pm

ahs3 wrote:
joevano wrote:Not to throw a big wrinkle into this.... but how would you run multiple maps? Each requires it's own instance of bzfs, which is unlike apache sites.

The same way, just start as many instances of bzfs or any proc, following each command with Ctrl+C. Then use exit to close the terminal. Or write a simple script to start them all, with a simple sleep timer to ensure they have time to start.
$ bzfs -conf cool.conf &
Ctrl+c
$ bzfs -conf cool2.conf &
Ctrl+c
$ exit
Right, but he wants to write startup scripts to treat bzfs like a service [like apache]. As far as I can think it through... they are all separate processes and writing something like the apache control script would/could get a bit hairy when dealing with multiple processes. Especially when you want to recycle a single map, without messing with ones that have current players. It can be done, just needs to be planned for.
There is nothing worse than aggressive stupidity. -- Johann Wolfgang von Goethe
"How many legs does a dog have if you call his tail a leg? Four. Calling a tail a leg doesn't make it a leg." -- Abraham Lincoln

User avatar
blast
General
General
Posts: 4736
Joined: Fri Mar 21, 2003 3:49 pm
Location: playing.cxx
Contact:

Re: Server CTL Script

Post by blast » Mon Aug 22, 2011 11:41 pm

ahs3 wrote:
$ bzfs -conf cool.conf &
Ctrl+c
$ bzfs -conf cool2.conf &
Ctrl+c
$ exit
Don't see what those Ctrl+c's are actually doing. You're already back at the shell when those bzfs instances start since they are being backgrounded. It's just likely that the server output makes you think the shell prompt isn't there. If you hit the Enter key or clear the terminal with Ctrl+L, you'll see a shell prompt again.
"In addition to knowing the secrets of the Universe, I can assure you that I am also quite potty trained." -Koenma (Yu Yu Hakusho)

Image

User avatar
ahs3
Private First Class
Private First Class
Posts: 315
Joined: Sun Mar 04, 2007 8:33 pm
Location: Press '/' to search
Contact:

Re: Server CTL Script

Post by ahs3 » Tue Aug 23, 2011 1:58 am

joevano, I misunderstood the wrinkle you spoke of, I was thinking of running multiple processes in the same terminal session.


blast, Ctrl+C serves the same purpose as Ctrl+L, without clearing the terminal, so there is a history of what I've done. I'm getting forgetful, it simply helps me to remember. :)

User avatar
blast
General
General
Posts: 4736
Joined: Fri Mar 21, 2003 3:49 pm
Location: playing.cxx
Contact:

Re: Server CTL Script

Post by blast » Tue Aug 23, 2011 9:16 am

No, it serves the same purpose as hitting the Enter key, since you're already at a shell prompt.
"In addition to knowing the secrets of the Universe, I can assure you that I am also quite potty trained." -Koenma (Yu Yu Hakusho)

Image

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest