简体   繁体   English

通过ssh将Java应用程序作为后台进程运行

[英]Run java application as background process via ssh

I'm currently developing a simple deployment script for vms running ubuntu. 我目前正在为运行ubuntu的vms开发一个简单的部署脚本。 All these machines are supposed to run a java application provided as a jar. 所有这些机器都应该运行以jar形式提供的Java应用程序。

This is the relevant part of the script installing java, copying a jar from local machine to remote machine and then starting the application: 这是安装Java,将jar从本地计算机复制到远程计算机,然后启动应用程序的脚本的相关部分:

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

the installation via the java_installation.sh script succeeds, the scp command does as well. 通过java_installation.sh脚本的安装成功,scp命令也是如此。 The problem occurs when trying to execute the commands in java_start_jar.sh via ssh. 尝试通过ssh执行java_start_jar.sh中的命令时,会发生问题。 java_start_jar.sh: 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

The scripts starts the execution of the .jar file, but then simply blocks. 脚本开始执行.jar文件,但随后只是阻止。 Ssh does not return, the rest of the local code is only executed after manually closing connection. ssh不返回,其余的本地代码仅在手动关闭连接后执行。 Any ideas why the java application is not properly running as a background process? 有什么想法为什么Java应用程序不能作为后台进程正确运行?

Move the & to the end of the line 将&移至行尾

#!/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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM