LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Executing multiple instances of a binary file from a single script? (https://www.linuxquestions.org/questions/linux-newbie-8/executing-multiple-instances-of-a-binary-file-from-a-single-script-737265/)

j_65_uk 07-02-2009 02:35 PM

Executing multiple instances of a binary file from a single script?
 
Hi guys,



I'm a Linux newbie and I've been using Debian 4.0 for a few months now, trying different things and getting to grasps with the basics.



My question is about ways of executing a binary (.bin) file from a script.



I've setup a gameserver for FEAR Combat. After copying the files into the home directory for the 'fear' user I created for the purpose, you then execute a script called start.sh which links to a library file then execute the game binary file (fearserver.bin).


This is the start.sh script



#!/bin/sh
export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH
./fearserver.bin


This all works fine, but then I tried to take things further and run 3 separate instances of the server on 3 separate ports.



I first tried calling/executing the binary 3 times in a row in the script, however what happens is that the first instance loads and runs and the script pauses there until you terminate the instance, and then the second instance is executed and so on...


#!/bin/sh
export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH
./fearserver.bin
./fearserver.bin
./fearserver.bin



My workaround (which does actually work) so far has been to create a separate start.sh script for each of the instances I'm trying to create, then I open up a remote console session to the Debian machine using Putty/SSH on Windows XP, login as the fear user I created, execute the script for the first instance of the binary, then close the session window without logging out. The .bin continues to run on the system. I then repeat this twice more and end up with the desired 3 instances of the binary all running AT THE SAME TIME under the fear user account.



So my question is can I get the same result launching the instances from the same script from a single session, rather than having to open a separate session to launch each instance?

Any help much appreciated!



Thanks

tredegar 07-02-2009 03:15 PM

I know nothing about fearserver but have you tried "putting in the background"?

With bash you can run a command "in the background", and not wait for it to complete / finish and terminate before you get the prompt back. So you can issue another command straight away. The process running "in the background" just carries on, but gives you your shell prompt back. Errors are generally reported back to the shell you are in, unless you redirect them.

You can do this by appending & to the command. Like this:
Code:

./fearserver.bin &
It's worth a try.

See man bash for the full story.

And Welcome to LQ!.

bigearsbilly 07-02-2009 03:35 PM

why multiples?
a good server should handle spawning instances itself usually.
do they not all try to bind the same port?
usually a service will have a startup script which you can activate on boot.

also the servers may die if you call them from an interactive
script and you close the window.

have you checked the docs to see if the server can daemonize properly?

FYI look at:

man at
man nohup

j_65_uk 07-02-2009 04:56 PM

Hi guys,

Tredegar - Adding the 'run in background' did the trick! That's exactly what I was after - useful technique to know and a nice simple answer :)

I've now created a single script that does everything.

Interestingly it does dump the 'initial' text output of each instance to the console, but this time all at once. There is no 'prompt' visible afterwards on the console, but as soon as I start typing it accepts it as a new command and the server instances disapear into the background. Just a quirk though, doesn't interfere with anything.

Bigearsbilly - The fearserver by default runs on port 27888. To change any of the settings (including the port) you have to specify an optionsfile when executing the file. I didn't mention this previously as it wasn't really relevant.

So my 'combined' startup script looks like this:

#!/bin/sh
export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH
./fearserver.bin -optionsfile /home/fear/FEAR/ServerOptions/ServerOptions-SMTDM $
./fearserver.bin -optionsfile /home/fear/FEAR/ServerOptions/ServerOptions-SMDM $
./fearserver.bin -optionsfile /home/fear/FEAR/ServerOptions/ServerOptions-SMCTF $

Having 3 instances lets me have the option of 3 different game modes. I could make the script run at startup but would prefer to start the servers manually...

I can confirm the 3 instances laucnhed from the single script are all running smoothly and are being thoroughly tested ;)

Thanks for all your help guys!


All times are GMT -5. The time now is 12:03 PM.