通过ssh将Java应用程序作为后台进程运行 [英] Run java application as background process via ssh

查看:144
本文介绍了通过ssh将Java应用程序作为后台进程运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为运行ubuntu的vms开发一个简单的部署脚本. 所有这些机器都应该运行以jar形式提供的Java应用程序.

I'm currently developing a simple deployment script for vms running ubuntu. All these machines are supposed to run a java application provided as a jar.

这是脚本的相关部分,安装Java,将jar从本地计算机复制到远程计算机,然后启动应用程序:

This is the relevant part of the script installing java, copying a jar from local machine to remote machine and then starting the application:

ssh ubuntu@$line -i ~/.ssh/key.pem -o StrictHostKeyChecking=no <java_installation.sh
scp -i ~/.ssh/key.pem $JARFILE ubuntu@$line:~/storagenode.jar
ssh ubuntu@$line -i ~/.ssh/key.pem <java_start_jar.sh

通过java_installation.sh脚本的安装成功,而scp命令也是如此. 尝试通过ssh执行java_start_jar.sh中的命令时,会发生问题. java_start_jar.sh:

the installation via the java_installation.sh script succeeds, the scp command does as well. The problem occurs when trying to execute the commands in java_start_jar.sh via ssh. java_start_jar.sh:

#!/bin/sh
# this script starts a jar file and creates a shellscript which can be used to stop the execution.
nohup java -jar ~/storagenode.jar & > ~/storagenode.log
pId=$!
echo "kill $pId" > ~/stop_storagenode.sh
chmod u+x ~/stop_storagenode.sh

这些脚本开始执行.jar文件,但随后只是阻塞. ssh不返回,其余的本地代码仅在手动关闭连接后执行. 为什么Java应用程序不能作为后台进程正确运行的任何想法?

The scripts starts the execution of the .jar file, but then simply blocks. Ssh does not return, the rest of the local code is only executed after manually closing connection. Any ideas why the java application is not properly running as a background process?

推荐答案

移动&到行尾

#!/bin/sh
# this script starts a jar file and creates a shellscript which can be used to stop the execution.
nohup java -jar ~/storagenode.jar > ~/storagenode.log &
pId=$!
echo "kill $pId" > ~/stop_storagenode.sh
chmod u+x ~/stop_storagenode.sh

这篇关于通过ssh将Java应用程序作为后台进程运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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