views:

246

answers:

1

I have a java application consisting of a Thread that monitors a table in the database that contains shell scripts to be executed. I look up the database, every few seconds. for a script, execute it (via the Process class), and wait for another script in the table.

My problem is: At times, some command in the script fails. The thread captures the error stream and logs it in the database. However, it keeps logging the messages and the control doesn't return from the script to the Java Thread due to which the subsequent shell scripts in the database are unable to execute.

Is there a work around for this?

+3  A: 

Are you reading both standard out and standard error ? Are you doing this in separate threads ? It's possible to block on one or the other depending on how you're consuming the script outputs, and I have a hunch that your script is blocked waiting for the parent process to consume its output.

From the Process documentation:

Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock

See this question and the sample code referencing 'stream gobblers'. 2 'gobbler's are created, consuming standard out and standard error.

Brian Agnew
I am reading both the streams in the same thread, and logging it. Just wanting to find a way to get out of the blocking mode, after the error occurs instead of keep printing it out.
Epitaph
You will *have* to read on separate threads, to keep consuming the output/error streams and prevent exec() from blocking. I suspect when you encounter an error, one of the streams blocks because it's not being consumed (stderr?) and hence you'll block.
Brian Agnew