Page 1 of 1

Server CTL Script

Posted: Sat Aug 20, 2011 12:57 pm
by Gamma Ray
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

Re: Server CTL Script

Posted: Sat Aug 20, 2011 2:48 pm
by blast
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.

Re: Server CTL Script

Posted: Sat Aug 20, 2011 4:24 pm
by Gamma Ray
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?

Re: Server CTL Script

Posted: Sat Aug 20, 2011 5:54 pm
by SkillDude
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.

Re: Server CTL Script

Posted: Sat Aug 20, 2011 10:02 pm
by Gamma Ray
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.

Re: Server CTL Script

Posted: Sat Aug 20, 2011 11:05 pm
by SkillDude
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.

Re: Server CTL Script

Posted: Sat Aug 20, 2011 11:19 pm
by Gamma Ray
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?

Re: Server CTL Script

Posted: Sat Aug 20, 2011 11:41 pm
by joevano
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...

Re: Server CTL Script

Posted: Sun Aug 21, 2011 12:33 am
by Gamma Ray
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?

Re: Server CTL Script

Posted: Sun Aug 21, 2011 12:44 am
by blast
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.

Re: Server CTL Script

Posted: Sun Aug 21, 2011 12:49 am
by Gamma Ray
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

Re: Server CTL Script

Posted: Sun Aug 21, 2011 1:32 pm
by blast
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.

Re: Server CTL Script

Posted: Sun Aug 21, 2011 9:44 pm
by SkillDude
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.

Re: Server CTL Script

Posted: Mon Aug 22, 2011 12:15 am
by ahs3
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.

Re: Server CTL Script

Posted: Mon Aug 22, 2011 2:23 am
by SkillDude
Ah, good point to bring up ahs3. This is why my "keepalive" scripts follow this general sequence:

Code: Select all

bzfs
./keepalive.sh &
exit

Re: Server CTL Script

Posted: Mon Aug 22, 2011 7:48 am
by joevano
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.

Re: Server CTL Script

Posted: Mon Aug 22, 2011 10:30 am
by ahs3
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

Re: Server CTL Script

Posted: Mon Aug 22, 2011 12:32 pm
by joevano
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.

Re: Server CTL Script

Posted: Mon Aug 22, 2011 11:41 pm
by blast
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.

Re: Server CTL Script

Posted: Tue Aug 23, 2011 1:58 am
by ahs3
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. :)

Re: Server CTL Script

Posted: Tue Aug 23, 2011 9:16 am
by blast
No, it serves the same purpose as hitting the Enter key, since you're already at a shell prompt.