执行Java代码中的另一个shell脚本内的shell脚本

huangapple go评论59阅读模式
英文:

Executing shell scripts within another shell script through Java code

问题

在我的Spring Boot应用程序中,我正在尝试触发一个shell脚本,该脚本内部会执行其他几个shell脚本。我能够使用ProcessBuilder触发我的主要脚本(比如master.sh),但是在master.sh内部调用的任何脚本都不会运行。实际上,什么都没有发生。

在高层次上,我正在尝试在我的master.sh中执行以下操作:

  • 检查某些条件
  • 执行另一个shell脚本 但这个从未执行过,因此后续的语句也没有执行。
  • 回调到我的Spring Boot应用程序进行curl调用
    ......

请帮忙解决,谢谢!

英文:

In my Spring-boot application, I am trying to trigger a shell script which in turns internally executes several other shell scripts.I am able to trigger my main script (say master.sh) using ProcessBuilder, but thereon any scripts being called within master.sh do not run. In fact, nothing happens.

At a high-level, I am trying to do the following in my master.sh -

  • Check for some condition
  • Execute another shell script This never executes hence no further statements execute.
  • make curl call back to my spring-boot app
    .....

Any help please...

答案1

得分: 0

我之前遇到的使用案例快速回顾 - 在我的 Spring Boot 应用中,用户有一个选项来触发一个主脚本(比如说 master.sh) - 这个主脚本在内部执行以下操作 -

(a). 在系统的其他位置执行另一个脚本 :: 这个脚本的执行时间可能在 30 分钟到 3 个多小时之间。

(b). 一旦步骤 (a) 完成,主脚本 master.sh 会运行 curl 命令,以回调到我的 Spring Boot 应用程序,以便对数据库执行更新操作。

上述步骤 a 和 b 在 master.sh 中会被多次执行。

解决方案:关于从 Java 代码触发任何命令,我使用了 ProcessBuilder 来实现相同的功能。但是,在调用 processBuilder.start() 之前,我不得不添加以下额外的语句以使其工作 -

processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);

上述语句将脚本输出重定向到父进程(在这种情况下是 Java)的错误和输出流中。

目前它对我起作用。如果行为发生变化(我不预期会发生),我将相应地更新本文。

最后,我想要求其他人分享他们对于为什么没有上述两个语句时我的内部脚本没有运行的理解。我确实理解这其中的原因,但找不到清晰的措辞来解释。谢谢大家。

英文:

Quick recap of the use case I was struggling with - In my spring-boot app, user has an option to trigger a main script (say master.sh) - this main script internally does the below -

(a). Executes another script present at some other location on the system :: this script could take anywhere between 30 mins - 3+ hours to execute.

(b). Once step (a) completes, master.sh (main script) runs curl commands to make a callback to my Spring boot application in order to perform updates to the DB.

Above steps a & b are executed multiple times inside the master.sh

Solution : As for triggering any command from Java code, i used ProcessBuilder to achieve the same. But I had to add below additional statements before issuing processBuilder.start() call in order to make it work -

processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);

Above statements redirect the script output to the parent process's (in this case Java) error and output streams.

At the moment it has worked for me. In case the behavior changes (which I don't anticipate) then I will update this thread accordingly.

Lastly, I would request others to share their understanding on why without the above two statements my internal scripts did not run. I do understand the "why" but not finding a clear wording to explain. Thanks All.

huangapple
  • 本文由 发表于 2020年9月21日 22:38:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63994532.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定