我有 bash 脚本来使用以下命令启动 java 进程:
#!/bin/bash
java -jar test.jar
我想制作一个脚本,这样如果这个 jar 的 java 进程死了,它将从这个脚本重新启动。我的脚本中是否需要一个 do while 循环,我应该使用什么条件来检查这个 jar 的 java 进程是否正在运行,如果死了则再次启动它。
最佳答案
以下将运行直到 test.jar
以 0 作为退出代码退出
while ! java -jar test.jar; do :; done
分割:
while ! java -jar test.jar; do :; done
# ^ ^ ^
# | | while loops needs a body, and `:` will do nothing
# | The command to run, while will check the exit code and continue if its 0 (true)
# Flip the exit code, so true becomes false and false becomes true
如果你有兴趣一直运行到test.jar
退出会出现falsy退出代码,你可以省略!
:
while java -jar test.jar; do :; done
关于linux - 如果java进程死于脚本,如何启动它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36618737/