LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-13-2010, 11:46 PM   #1
pavanlimo
LQ Newbie
 
Registered: Sep 2004
Location: Dharwad, India
Distribution: Mandrake
Posts: 3

Rep: Reputation: 0
Question 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?
 
Old 08-14-2010, 10:43 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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.
 
1 members found this post helpful.
Old 08-15-2010, 02:20 AM   #3
pavanlimo
LQ Newbie
 
Registered: Sep 2004
Location: Dharwad, India
Distribution: Mandrake
Posts: 3

Original Poster
Rep: Reputation: 0
Arrow

Quote:
Originally Posted by paulsm4 View Post
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.
 
0 members found this post helpful.
Old 08-15-2010, 01:34 PM   #4
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 08-16-2010, 01:14 PM   #5
pavanlimo
LQ Newbie
 
Registered: Sep 2004
Location: Dharwad, India
Distribution: Mandrake
Posts: 3

Original Poster
Rep: Reputation: 0
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.

Last edited by pavanlimo; 08-16-2010 at 01:15 PM.
 
Old 08-16-2010, 03:49 PM   #6
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
  


Reply

Tags
exec, java, shell script


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
shell script execution on double click havejeet Linux - Newbie 6 06-05-2010 03:08 AM
How to control execution of programs in a shell script. Chrisantha1 Programming 5 04-28-2010 03:29 AM
Shell script hangs junior_unix_user Linux - Newbie 8 04-07-2010 08:53 AM
Remove external script execution in C, C++ and Java Sinedie Linux - Security 15 08-24-2008 07:35 PM
Speeding up Shell Script execution?? funkymunky Programming 8 07-16-2004 08:39 PM


All times are GMT -5. The time now is 01:49 AM.

Main Menu
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