Executing shell scripts from a Java application in

2019-06-01 08:53发布

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?

标签: java shell
1条回答
爷的心禁止访问
2楼-- · 2019-06-01 09:12

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.

查看更多
登录 后发表回答