• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

using the Process class to read process exit status

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm having a weird issue using the Process class. I'm using it to invoke a shell script and read it's exit status. The java method we're using is Process.waitFor().

The weird bit is that the exit status is not always correctly returned by Process.waitFor(). Many times it returns a 13 regardless of what was actually returned by the script. To make things weirder I've noticed that this happens if within the script I call a function.

Here's how I can reproduce. First have a script "/tmp/myscript.sh" that contains



and here's my java code:




and the main class



Now if you run Main, it should output a 13, (or sometimes a 3 for some odd reason, I believe it may have to do with it believing there's a syntax error).

I've noticed that if I change the command string to be "sh /tmp/myscript.sh", that it correctly returns a 0. The problem is we would rather not break our ProcessUtility class by always prepending an sh if this is not necessary.

Another thing I've noticed is that if the /tmp/myscript.sh contains



Then again, this problem does not occur.

I'm looking for any insight into what is causing the strange behavior. And if there is any way to resolve it within the script, while still being able to define "daFunc". Any thoughts are appreciated.

-Dan
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code was a little too large for me to go through. Is it possible that the script by itself returns a value of 13 ?
 
D Rodriguez
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. I'm sorry for posting too much code. That probably didn't help.

When I run either of the script's alone from the shell, and then type "echo $?" they both return a 0. I'm guessing that this maybe works slightly differently than when using the Process class.

-Dan
 
D Rodriguez
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I edited my original post with a clearer explanation. Please see above. Any help is greatly appreciated.

-Dan
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

D Rodriguez wrote:I've noticed that if I change the command string to be "sh /tmp/myscript.sh", that it correctly returns a 0. The problem is we would rather not break our ProcessUtility class by always prepending an sh if this is not necessary.


If the script is executable you shouldn't need to call a shell directly. The operating system should take care of that for you.


Another thing I've noticed is that if the /tmp/myscript.sh contains



Then again, this problem does not occur.


I think it's a good idea to always return some explicit value. I don't know which shell you are actually using for executing the script (sh, bash, zsh, etc), but perhaps sh returns 0 when nothing is returned explicitly and the actual shell returns something random.
 
Yeah, but is it art? What do you think tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic