将jar作为Linux服务运行 - init.d脚本开始应用程序卡住 [英] Running jar as a Linux service - init.d script gets stuck starting app

查看:215
本文介绍了将jar作为Linux服务运行 - init.d脚本开始应用程序卡住的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将一个可运行的jar作为Linux VM盒上的后台服务来实现。我使用此处作为基础进行处理的示例,并修改了start()进入此方法:

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


start(){

start() {

#Start application

# Start application

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

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

PID = $!

echo $ PID> pid.txt

echo $PID > pid.txt

}

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

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.

这是启动命令的处理程序:

Here is the handler for the start command:


case$ 1开始时)

case "$1" in start)

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


当我调用方法时,我得到Starting Sagepay-stub 输出,但后来我陷入了剧本。我所能做的就是Ctrl& C退出,这给了我输出:

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:


Sagepay-stub启动。

"Sagepay-stub started."

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

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 :

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.

如果问题出在PID命令(我在另一个线程上找到的接受答案),我该如何评论这些命令并仍能停止服务?

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?

对代码的评论也欢迎

谢谢

推荐答案

你有没有忘记'java -jar [application stuff]'之后的'&',以便在该行之后继续执行。

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

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

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

这就是它打印Sagepay-stub started的原因。一旦你点击ctrl-c,因为那是你杀死java进程并继续bash脚本。

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.

这篇关于将jar作为Linux服务运行 - init.d脚本开始应用程序卡住的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆