LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Java hangs even though shell script’s execution is completed (http://www.linuxquestions.org/questions/programming-9/java-hangs-even-though-shell-script%92s-execution-is-completed-826140/)

pavanlimo 08-14-2010 12:46 AM

Java hangs even though shell script’s execution is completed
 
I'm trying to execute a script from within my java code. The execution of the script is over(it's pid is no more), but java is stuck on waitFor() method of the shell script process!. And yes, I'm reading output and error streams in 2 separate threads. Yes, they are being joined at the end(after waitFor()).

The aspect which makes this question relevant to this forum is, if I put the following line in the shell script(at the top), everything seems to work fine.

Code:

exec 3>&1 > /tmp/some_log 2>&1
And no, interestingly enough, the script doesn't generate any output!. Zero chars!. So putting exec statement there makes no sense!

But still, magically enough, putting exec statement in the script makes java work!. Why??

How can I avoid that illogical exec statement in the script?.

exec has any other functionality here, other than redirection?

paulsm4 08-14-2010 11:43 PM

Dude - how are WE supposed to figure out what either your Java program or your shell script are supposed to be doing???

Yes, it's possible to call a shell script from a Java program. Easy, in fact!
Yes, generally, you should be able to see anything that's printed to stderr or stdout.
And yes, you should be able to redirect stderr and/or stdout to a file.

SUGGESTION:
1. Try reproducing the problem with a "simple-as-possible" test program
2. If, at that point, you still can't figure out, then post the COMPLETE contents of your (small!) test program and (even smaller!) script.

pavanlimo 08-15-2010 03:20 AM

Quote:

Originally Posted by paulsm4 (Post 4066624)
Dude - how are WE supposed to figure out what either your Java program or your shell script are supposed to be doing???

Yes, it's possible to call a shell script from a Java program. Easy, in fact!
Yes, generally, you should be able to see anything that's printed to stderr or stdout.
And yes, you should be able to redirect stderr and/or stdout to a file.

SUGGESTION:
1. Try reproducing the problem with a "simple-as-possible" test program
2. If, at that point, you still can't figure out, then post the COMPLETE contents of your (small!) test program and (even smaller!) script.

Thanks for your attention. I had a discussion here.

Posted the link for the benefit of others, if they ever face such a situation.

paulsm4 08-15-2010 02:34 PM

pavanlimo -

You're just asking a bunch of vague questions and making a bunch of totally unwarranted guesses.

Not the best way to solve a problem, IMHO.

My suggestion remains: try to reproduce the problem with a minimal test case, then DETERMINE what the problem is. Don't GUESS.

PS:
While you're at it, you might want to look into "buffered I/O'.

Why it's a Good Thing.

And why it might lead to sometimes "unexpected" results.

Two clues:
1. "stdout" (stream 1) is buffered; "stderr" (stream 2) isn't.
Try writing to stderr instead of stdout in your program and see if you get different results.

2. When a program ends (*really* ends), all buffered output is flushed (unless, of course, it terminates abnormally)(which is probably not the case here).

In any case, try to THINK about what you're doing. Don't just arbitrarily throw stuff at the wall and jump to false conclusions
about what happens to stick.

IMHO .. PSM

pavanlimo 08-16-2010 02:14 PM

I don't think I'm "throwing stuff at the wall and jumping to false conclusions".
Nevertheless I thank you for your time and response. Appreciate it.
I had a good discussion with other guys, ultimately got a solution.
Please see the solution here.

Thanks.

paulsm4 08-16-2010 04:49 PM

Hi, pavanlimo -

Thank you for the followup, and sorry if I was a bit hard on you :(

More important, I'm glad you're squared away :)

Sincerely .. PSM


All times are GMT -5. The time now is 09:57 PM.