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 |
You can background a command by putting an & at the end:
Code:
#!/bin/bash Code:
url=$(java StartServer) & |
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:
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 HTH. Let us know. |
Thanks. Please find my answers inline:
Quote:
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 |
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? |
You could possibly make the second command read STDIN for the URL. E.g.:
Code:
unbuffer java StartServer | java UseServer |
Quote:
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 &) HTH. Let us know. |
All times are GMT -5. The time now is 07:48 PM. |