LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 08-28-2012, 07:54 PM   #1
tatarin
LQ Newbie
 
Registered: Mar 2008
Posts: 10

Rep: Reputation: 0
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
 
Old 08-28-2012, 08:17 PM   #2
byannoni
Member
 
Registered: Aug 2012
Location: /home/byannoni
Distribution: Arch
Posts: 128

Rep: Reputation: 36
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
 
Old 08-28-2012, 08:46 PM   #3
ShadowCat8
Member
 
Registered: Nov 2004
Location: Arcadia, CA
Distribution: Gentoo, Arch, (RedHat4.x-9.x, FedoraCore 1.x-4.x, Debian Potato-Sarge, LFS 6.0, etc.)
Posts: 209

Rep: Reputation: 43
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.

Last edited by ShadowCat8; 08-28-2012 at 08:53 PM. Reason: Added shell code option
 
Old 08-28-2012, 11:38 PM   #4
tatarin
LQ Newbie
 
Registered: Mar 2008
Posts: 10

Original Poster
Rep: Reputation: 0
Thanks. Please find my answers inline:

Quote:
Originally Posted by ShadowCat8 View Post
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
 
Old 08-29-2012, 01:59 PM   #5
tatarin
LQ Newbie
 
Registered: Mar 2008
Posts: 10

Original Poster
Rep: Reputation: 0
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?
 
Old 08-29-2012, 04:09 PM   #6
byannoni
Member
 
Registered: Aug 2012
Location: /home/byannoni
Distribution: Arch
Posts: 128

Rep: Reputation: 36
You could possibly make the second command read STDIN for the URL. E.g.:
Code:
unbuffer java StartServer | java UseServer
 
Old 08-29-2012, 05:15 PM   #7
ShadowCat8
Member
 
Registered: Nov 2004
Location: Arcadia, CA
Distribution: Gentoo, Arch, (RedHat4.x-9.x, FedoraCore 1.x-4.x, Debian Potato-Sarge, LFS 6.0, etc.)
Posts: 209

Rep: Reputation: 43
Quote:
Originally Posted by tatarin View Post
Quote:
Originally Posted by ShadowCat8 View Post
...
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.

Last edited by ShadowCat8; 08-29-2012 at 05:45 PM. Reason: Added the while loop
 
  


Reply

Tags
java, jvm, process, shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to ssh in two machine one after another (in sequence) using shell script kushkothari Linux - Networking 21 10-21-2008 11:03 PM
Return code of a shell script zachos.v Linux - Software 3 10-10-2006 10:51 AM
Return value to shell in script mwade Linux - Newbie 11 08-18-2006 10:49 AM
Creating a shell script to run Java program paultaylor Programming 7 11-12-2004 04:11 PM


All times are GMT -5. The time now is 01:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration