Java hangs even though shell script’s execution is completed
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.