LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell script needs to run two java binaries in sequence .. first does not return (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-needs-to-run-two-java-binaries-in-sequence-first-does-not-return-4175424493/)

tatarin 08-28-2012 06:54 PM

Shell script needs to run two java binaries in sequence .. first does not return
 
I need to run Java binary from shell script which starts a server and then another binary which uses the started server's url so the first script needs to return the url. The problem is that when I run the script from command line the StartServer program never finishes though it bring up the server and everything but it's just never goes to the next command which is UseServer and just sits there. How do I force it to go to the next command without killing the server. System.exit() kills JVM and all processed in it.

Here is the Java binary:

import env;

public class StartServer {
public static void main(String[] args) {
env.startServer();
env.getServerUrl();
return;
}
}


Shell script:

#!/bin/bash

# start server
url=$(java StartServer)

# use the url
java UseServer url

byannoni 08-28-2012 07:17 PM

You can background a command by putting an & at the end:
Code:

#!/bin/bash
url=$(java StartServer) &
java UseServer url

However, be careful to make sure that the first command finishes printing the URL before continuing:
Code:

url=$(java StartServer) &
sleep 5
java UseServer url


ShadowCat8 08-28-2012 07:46 PM

Greetings,

I can think of a way to accomplish this, but I also see some potential issues with doing it, too.

If you don't mind me asking a few questions regarding this:
  1. What tells an administrator (assuming it's an administrator that is starting the server) that the server is currently running on the box (without having to comb through 'ps ax' output)? Is any pid or lock file being created by the Java server?
  2. What would happen to the server if another instance of the server was started?
  3. Is there a reason that the server/service isn't being started from an init script?

And, my answer to the question would be to use 'screen'. That way you can start and detach the terminal session you started the server on, but then you have to remember to reattach it to shut it down when you are done with it.

Another way to do it might also be to use something like this in the shell script, but YMMV:
Code:

#!/bin/bash

if `url=$(java StartServer)`
    then
    java UseServer url
    fi



HTH. Let us know.

tatarin 08-28-2012 10:38 PM

Thanks. Please find my answers inline:

Quote:

Originally Posted by ShadowCat8 (Post 4766933)
Greetings,

I can think of a way to accomplish this, but I also see some potential issues with doing it, too.

If you don't mind me asking a few questions regarding this:
  1. What tells an administrator (assuming it's an administrator that is starting the server) that the server is currently running on the box (without having to comb through 'ps ax' output)? Is any pid or lock file being created by the Java server?
    Well, I'm looking for a streaming logs in the terminal and once the server is started there is a message saying something like "The server is running at http://localhost:12345". No lock file is being created as far as I know.
  2. What would happen to the server if another instance of the server was started?

    Only once instance of the server can be started at a time.
  3. Is there a reason that the server/service isn't being started from an init script?
    The way those two Java binaries are setup (not by me) is that there is no way for me to pass the url as JVM flag from first binary to the second so the only way is to do it in the shell script.


HTH. Let us know.

So the problem with the solution you provided is that url=$(java StartServer) will never happen because once the server is started the process will continue to run and shell script does not know to move on to the next command. Like I mentioned it just waits and waits and waits.

The first person (byannoni) provided another solution but the problem with that one is that I don't know how long it will take for a server to start. Of course I can always put up to 1 minute sleep but that seems like a flaky solution.

thanks

tatarin 08-29-2012 12:59 PM

I have tried both suggestions and it's still not working.

Once the server is started the shell does not understand that it needs to go to the next command now and just sits there without doing anything. Basically nothing is happening after the line in the terminal "The server is running at http:// blah blah".

Any ideas how can I create a signal that server is started and force shell to move on to the next process?

byannoni 08-29-2012 03:09 PM

You could possibly make the second command read STDIN for the URL. E.g.:
Code:

unbuffer java StartServer | java UseServer

ShadowCat8 08-29-2012 04:15 PM

Quote:

Originally Posted by tatarin (Post 4767019)
Quote:

Originally Posted by ShadowCat8 (Post 153348)
...
2. What would happen to the server if another instance of the server was started?

Only once instance of the server can be started at a time.


Okay, so how does it know? Does it check if the port is in use? At what point does it know that an instance of the server is already running and what does it say out to the terminal when it realizes this?

The answers to these follow-up questions may give you the item to key on to know when to launch the second command. Then you can put that in an 'if' or a 'while' statement and be fairly sure that it will run as expected.

And, as a thought, you *could* do something like this (using the port "12345" from your earlier example):
Code:

url=$(java StartServer &)

while [ "`netstat -n | grep '127.0.0.1:12345' 1>/dev/null ; echo ${?}`" -gt "0" ]
          do
          echo "Still waiting for the server to initialize..."
          done

java UseServer $url &

It's kind of messy and sloppy with a lot of unnecessary output, but it *should* work... Conceivably. ;) hehe

HTH. Let us know.


All times are GMT -5. The time now is 07:48 PM.