1

I am experimenting with running Spring Boot apps on a remote CentOS 7 web server which has been successfully serving up apps on Apache and a stand-alone tomcat instance since day one. However, when I ssh to the remote server and launch a Spring Boot app (which is in a jar that contains its own embedded tomcat instance), the Spring Boot web service jar runs successfully while my ssh connection is live, but then terminates after my ssh session ends. What specific steps do I have to take to get the jar containing the Spring Boot web service to continue running after my ssh session is terminated?

Here is what I typed to start the app using an ssh session from my devbox many hundreds of miles away:

[ ~]$ cd /path/to/webservice
[ webservice]$ kill $(lsof -t -i:9000)
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
[ webservice]$ java -jar mywebservice.jar

Since apache forwards requests for the service internally to port 9000, the service is then exposed to the outside world via an apache url somedomain.com/someurlpattern until I terminate the ssh session.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
CodeMed
  • 5,199
  • 2
    Try screen (http://unix.stackexchange.com/questions/34321/leave-remote-command-running-storing-output) or nohup and & (http://unix.stackexchange.com/questions/3886/difference-between-nohup-disown-and) – Jeff Schaller Dec 30 '15 at 19:22

1 Answers1

2

Running a process in background and closing ssh session

There are many ways to do so:

1.The nohup command

You can use the nohup command to execute commands after you exit from a shell prompt.

Example:

  $ nohup java -jar mywebservice.jar > /path/to/webservice/logs/mylogfile.txt &

  ## exit from shell or close the terminal ##
  $ exit

2.The disown bash command

Another option is to use the disown command as follows:

Example:

  $ java -jar mywebservice.jar > /path/to/webservice/logs/mylogfile.txt &
  [1] 10685
  $ disown 10685
 $ ps
    PID TTY          TIME CMD
    10685 pts/0    00:00:00 wget
    10687 pts/0    00:00:00 bash
    10708 pts/0    00:00:00 ps
 $ logout

3.The screen command

You can also use the screen command for this purpose.

Mohammad
  • 688
Ijaz Ahmad
  • 7,202
  • Answer updated , that should work – Ijaz Ahmad Dec 30 '15 at 20:41
  • Thank you and +1 for taking the time to look into this. I closed the terminal window containing the ssh session. I also used who and pkill ... to terminate ssh sessions. I then tested to see the web service is still working. But I want to wait a little while to confirm that the service is not running simply due to a session that has not timed out yet. I am somewhere on SE most days, so I will make a note to check in an answer this later. You can ping me to reach me if you need to. Thank you again. – CodeMed Dec 30 '15 at 21:06
  • 1
    The service was still working today, so I am marking this as accepted. Thank you again for helping solve this problem. – CodeMed Jan 01 '16 at 01:02