Keeping a Java app running when SSH is closed on Google Cloud

11

1

Right now I’m running a Java app by SSH-ing to a VM on Google Cloud Compute. I issue a command to run the app and it loads a console. As soon as I terminate the SSH connection, the app shuts down. I assume this is because the console has been closed.

I would like this app (which is a server) to persist after I terminate SSH, and, at any point, I would like to be able to reconnect to the console and issue commands on the server.

Noahb32

Posted 2016-08-06T16:35:12.463

Reputation: 113

1

Possible duplicate of How I do run gnome app and let it run via SSH even after SSH closed?

– Jakuje – 2016-08-06T20:42:32.620

Answers

12

You state this:

I would like this app (which is a server) to persist after I terminate SSH, and, at any point, I would like to be able to reconnect to the console and issue commands on the server.

Slightly vague in goals, but I will take a stab at it!

To run any command as a background process on a Linux server, you should prepend the command with nohup and append & to the end.

So the final command would be:

nohup [your command] &

The nohup means the command should ignore “hang ups” and the ampersand & appended to it is a shell command that tells the system to run the command as background process. More information on it’s usage can be found here.

When you run a command like this, the process will run, you will be sent back to the command prompt and you can exit the terminal session or even go and do something else unrelated to that command during that terminal session.

JakeGould

Posted 2016-08-06T16:35:12.463

Reputation: 38 217

1If I want to return to the process and enter a command into the java console, how would I do that? – Noahb32 – 2016-08-06T19:02:42.653

1

@Noahb32 Well, this would launch it as a background process; I mostly use this method to just launch a process and then forget it. If you wanted to start a new Java console, you could just do that since you are back in the shell. But maybe you should investigate using screen instead since it allows you to get back into the session after launch. Honestly your question is a big vague so if you could clarify it, that might help clear things up.

– JakeGould – 2016-08-06T23:07:04.817

1Thank you! I'd like to add that you can monitor output from another terminal: just run tail -f nohup.out in the same work. dir. – naXa – 2018-10-19T20:47:21.760

3

There are at least three ways to do this: nohup ('no hang up'), screen/tmux, or disown. It's a little unclear from you question as to your need to connect to the server and interact with the daemon after you disconnect.

If this is the case, nohup and disown won't be good matches as you release the process (though it keeps running). tmux (and also screen) will keep a term running even when you.

For more info you can see the respective pages:

man nohup
man tux

Bash disown (assuming you are running Bash, though many other shell include something similar)

user1794469

Posted 2016-08-06T16:35:12.463

Reputation: 256

Seems like there is no disown command on Google Cloud, at least the instance I'm using, started 2019 or late 2018. The nohup command works fine, but it does create an extra file which may not be wanted. – trysis – 2019-11-30T20:23:04.117

1

To detach an application from its running shell, making it persist after the shell exits, use the nohup command.

You simply prefix your normal command with nohup and it should work.

Julie Pelletier

Posted 2016-08-06T16:35:12.463

Reputation: 2 065