简体   繁体   中英

Run java application as background process via ssh

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.

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

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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