Java 将 jar 作为 Linux 服务运行 - init.d 脚本在启动应用程序时卡住

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19248365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:26:43  来源:igfitidea点击:

Running jar as a Linux service - init.d script gets stuck starting app

javalinuxservicejarlocalhost

提问by a.hrdie

I am currently working on implementing a runnable jar as a background service on a Linux VM box. I have used the example found hereas a base to work on, and have modified the start() method into this:

我目前正在致力于在 Linux VM 机器上实现一个可运行的 jar 作为后台服务。我已经使用这里找到的示例作为工作的基础,并将 start() 方法修改为:

start() {

# Start application

java -jar /home/vagrant/sagepay-stub-1.4.jar >/var/log/sagepay-stub.log 2>&1

PID=$!

echo $PID > pid.txt

}

开始() {

# 启动应用

java -jar /home/vagrant/sagepay-stub-1.4.jar >/var/log/sagepay-stub.log 2>&1

PID=$!

回声 $PID > pid.txt

}

This sets up the service to write output to the log sagepay-stub.log and saves the PID for use when the service stop method is called.

这将设置服务以将输出写入日志 sagepay-stub.log 并保存 PID 以在调用服务停止方法时使用。

Here is the handler for the start command:

这是 start 命令的处理程序:

case "$1" in start)

echo "Starting $APP"
start
echo "$APP started."
;;

案例“$ 1”在开始)

echo "Starting $APP"
start
echo "$APP started."
;;

When I call the method i get the "Starting Sagepay-stub" output, but then I am stuck inside the script. All i can do is Ctrl & C to quit, which gives me output:

当我调用该方法时,我得到了“Starting Sagepay-stub”输出,但随后我被困在脚本中。我所能做的就是 Ctrl & C 退出,这给了我输出:

"Sagepay-stub started."

“Sagepay-stub 开始了。”

Now i look in the logs and see that the output is as expected - the stub server has started successfully. But i cannot wget on the port (i have opened the relevant ports using iptables) - the connection is refused :

现在我查看日志并看到输出符合预期 - 存根服务器已成功启动。但是我无法在端口上 wget(我已经使用 iptables 打开了相关端口) - 连接被拒绝:

Connecting to localhost|127.0.0.1|:8889... failed: Connection refused.

Any ideas of what the problem is are appreciated. I think the problem lies with starting the app then moving on and letting it run in the background. The script get stuck waiting for something while the app starts. The jar runs fine locally without input.

任何关于问题所在的想法都值得赞赏。我认为问题在于启动应用程序然后继续并让它在后台运行。当应用程序启动时,脚本会卡住等待某些事情。jar 在没有输入的情况下在本地运行良好。

If the problem lies with the PID commands (which I found on another thread as an accepted answer) how can I comment these out and still be able to stop the service?

如果问题出在 PID 命令上(我在另一个线程上发现它作为已接受的答案),我该如何注释掉这些命令并仍然能够停止服务?

Comments on code also welcome

也欢迎对代码的评论

thanks

谢谢

采纳答案by LiquidityC

Haven't you forgotten a '&' after the line 'java -jar [application stuff]' so that execution continues after that line.

您是否忘记了“java -jar [application stuff]”行之后的“&”,以便在该行之后继续执行。

It's exactly the same as if you run the command in console. A '&' after ensures that the console is usable after application start.

这与在控制台中运行命令完全相同。之后的“&”确保控制台在应用程序启动后可用。

That is why it prints "Sagepay-stub started." once you hit ctrl-c because that is when you kill the java process and the bash script continues.

这就是为什么它会打印“Sagepay-stub started”。一旦你按下 ctrl-c,因为那是你杀死 java 进程并且 bash 脚本继续的时候。