Run java application as background process via ssh - 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.
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

Related

Run spring boot shell as daemon

I have an Spring boot web-application with an interactive shell for configuration. I want to run my app as daemon to start server. When I run it normally using java -jar app.jar I get an interactive shell and also I can connect to server using web-browser. but sometimes I want to run it as background on a VPS.
When I run it using nohub with a bash script like following it run but not as daemon and when I close SSH it stop working.
#!/bin/sh
nohup java -jar /web/server.jar &
When I run it as service using ln -s /var/jar-file /etc/init.d/app and call service app start it get error and couldn't run.
When I run it using java -jar app.jar > /var/log/app.log 2>&1 it starts well but by closing the SSH it stop.
You probably have to disable the interactive shell with:
spring.shell.interactive.enabled=false

Run jar on remote machine as background process over ssh

I want to run a jar as a background process on a remote machine over ssh connection. There is bash script on remote machine to execute jar
#!/bin/sh
export JAVA_HOME=/location/of/java/
export PATH=$JAVA_HOME/bin:$PATH
nohup java -jar jar_name.jar config.properties &
If I execute the above script directly from remote machine(sudo ./start_script.sh), jar is started as background process and stdout is directed to nohup.out in the same folder as jar. But when I run script from local machine : ssh vm_name 'sudo ./start_script.sh', the process starts up. but it blocks and the output is directed to local terminal.
is there way to achieve this?
EDIT: I need to run script as root and also pass parameters to script, added placeholder path for JAVA_HOME to avoid confusion
You need to tell the ssh to connect as a terminal.
ssh vm_name -t 'sudo ./start_script.sh'
It's likely recognizing that you are not on as a terminal and altering behavior accordingly.
Same issue related here:
https://serverfault.com/questions/955268/what-is-the-difference-between-running-a-command-in-ssh-shell-manually-vs-runnin
Try
$ ssh user#host bash -l ./build.sh

Running Executable jar file keep hanging on Jenkins

I am very newbie in Jenkins, I've created Jenkins job where it will execute somekind like this jar command in Jenkins job:
ECHO **********************************
ECHO Upgrade Devices
ECHO **********************************
cd c:\JenkinsPull
java -jar DevicesUpgrade.jar PC1 %ProjectName%
When I start to build the Jenkins job it able to run the DevicesUpgrade.jar file until last part of jar process before it finish, sudddenly it just stuck there till I abort the job.
So I think there must be a bug inside the jar file, but it is weird because after that, I tried to run java -jar DevicesUpgrade.jar PC1 %ProjectName% on command prompt, it didn't stuck at all and it able to finish up the jar.
Is there anything that I need to do on Jenkins to fix this hang problem?
Here is the output I got from Jenkins:
c:\JenkinsPull>java -jar DevicesUpgrade.jar PC1 lex_l11_gms
%%%%f95eac91%%%%%
lexl11g_64-ota-L11_D01.00.48_userdebug.zip
1517844524458
%%%%%%%%%%%%%%%%%%%%%%%%
adb -s shell f95eac91 "getprop | grep ro.build.version.incremental"
\\ANDROIDTEST-05\ARM_png\JenkinsPull\BSP\lex_l11_gms\lexl11g_64-ota-
L11_D01.00.48_userdebug.zip
1.143848721E9
[*--------------------------------------------------------------------------
--------------------------]
Here it keep loading the jenkins job till I need to abort the Jenkins job.
Here is the output I got when I am running the jar via Command Prompt:
c:\JenkinsPull>java -jar DevicesUpgrade.jar PC1 lex_l11_gms
%%%%f95eac91%%%%%
lexl11g_64-ota-L11_D01.00.48_userdebug.zip
1517844524458
%%%%%%%%%%%%%%%%%%%%%%%%
adb -s shell f95eac91 "getprop | grep ro.build.version.incremental"
\\ANDROIDTEST-05\ARM_png\JenkinsPull\BSP\lex_l11_gms\lexl11g_64-ota-
L11_D01.00.48_userdebug.zip
1.143848721E9
[*--------------------------------------------------------------------------
--------------------------]
Upgrade Device Completed..!!
c:\JenkinsPull>

Ansible terminates my app with "nohup"

I have a task in Ansible playbook that executes a .jar file in the background, but after finishing that task, the (java) app terminates.
- name: Run Java app in the background
shell: nohup java -jar app.jar &
I need the app running for tasks further down in the playbook. Any ideas??
NOTE: When I run it in Putty ssh session it runs smoothly and the app stays in background.
The most likely reason is attached IO. Try:
- name: Run Java app in the background
shell: nohup java -jar app.jar </dev/null >/dev/null 2>&1 &
Ok So I had this issue today and my script is a tad different but essentially doing the same thing so this is what I have, running on Oracle, Centos and RHEL.
in script start_service.sh I have this, this script is executed not sourced.
#!/bin/sh
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"
nohup java -jar ${DIR}/$1 --spring.config.location=${DIR}/application.yml --logging.config=${DIR}/logging.xml > ${DIR}/log/output.log 2>&1 &
In my playbook I have:
- name: run service
shell: "{{ service_install_location }}/application_name/start-service.sh application_name-{{ application_version }}.jar"
args:
chdir: "{{ service_install_location }}/application_name"
executable: /bin/bash
Now, the BASH_SOURCE argument is specific to bash so if you are running plain old sh then you need $0. nohup is required so when the ssh connection terminates your background task does not get killed (turn on -vvv for ansible to see connections being made to understand how this happens on a remote machine).
I should point out we are doing this temporarily as this should really be installed as a service to be under the control of service/init.d but for now, this answers your question

Log output shows in Jenkins while running bash script to execute Java code

At first let me describe my issue.
I configured Jenkins and after build action I called shell script to run bash script on remote server.
The shell script starts application via command
java -Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=xxx
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-XX:+HeapDumpOnOutOfMemoryError -jar name.jar "BUILD_PARAMETER"
I see logs from my application in Jenkins build, and it's keep build process running. I need to finish it after running
sh run command. Is it possible?
If you're doing this using Jenkins you will need to use the nohup notation as in the comments as well as specifying a non-numerial PID for the process. Jenkins tries to clean up after a job finishes by killing any processes it starts.
BUILD_ID=dontKillMe nohup <-your command -> &
the above command should work
https://wiki.jenkins-ci.org/display/JENKINS/Spawning+processes+from+build
Your shell script need to fork a process, and return, otherwise Jenkins thinks your shell script is still running (which it is, if it's not forking the process and returning).
You have not provided the command you use to launch your application, but a common way to fork a process in linux is:
nohup <your command here> &

Categories