Kristaphonie
Kristaphonie

Reputation: 99

Run SSH command nohup then exit from server via Jenkins

So I've tried googling and reading a few questions on here as well as elsewhere and I can't seem to find an answer.

I'm using Jenkins and executing a shell script to scp a .jar file to a server and then sshing in, running the build, and then exiting out of the server. However, I cannot get out of that server for the life of me. This is what I'm running, minus the sensitive information:

ssh [email protected] 'killall -9 java; nohup java -jar /root/project.jar -prod &; exit'

I've tried doing && exit, exit;, but none of it will get me out of the server and jenkins just spins for ever. So the Jenkins build never actually finishes.

Any help would be sweet! I appreciate it.

Upvotes: 1

Views: 6170

Answers (3)

Johan
Johan

Reputation: 425

Try this. This is working for me. Read the source for more information.

nohup some-background-task &> /dev/null   # No space between & and > !

Example :

ssh [email protected] 'killall -9 java; nohup java -jar /root/project.jar -prod &> /dev/null'

no need exit keyword

source : https://blog.jakubholy.net/2015/02/17/fix-shell-script-run-via-ssh-hanging-jenkins/

Upvotes: 0

Kristaphonie
Kristaphonie

Reputation: 99

So I just took off the exit and ran a ssh -f [email protected] before the command and it worked. The -f just runs the ssh command in the background so Jenkins isn't sitting around waiting.

Upvotes: 4

Slav
Slav

Reputation: 27485

Usual way of starting a command and sending it to background is nohup command &

Upvotes: 1

Related Questions