java 调用本地 shell,独立地从Java应用程序执行shell脚本

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?

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值