It turns out that I have a process that I would like to keep open for an indefinite amount of time. If I start the process from SSH, exiting the SSH session also kills the process.
I want to know how that process can be left running even though I exit the SSH session.
Clarifications:
tmux
andscreen
they're not primarily for what you want, they're a multiplexer and a terminal emulator respectively. Allowing you to keep a process running on a remote server is not its main feature. To use them only for that is to underutilize them and leave too many programs running on your remote server.Contrary to what @patapalo said, the '&' character does not keep a process open after logging out. What it does is it passes it to the background allowing the shell it was run from to continue working. If you close the ssh connection this process stops.
What you need is the program
nohup
that is designed for that.The way to run nohup is as follows:
What it does makes
nohup
a program ignore the SIGHUP signal. When you run a program and then exit the terminal, even though you ran it in the background with./programa &
, the program receives a SIGHUP signal which suspends execution of the program. Instead, withnohup
you can continue running the program despite closing the ssh session or sending a signal likekill -1 pid_del_programa
.To kill it, you just have to kill the process associated with the program, either with kill (of course, as long as you don't send the 1 or
SIGHUP
) signal, or with some process viewer, etc.You can run the process using
screen
:and then you do a detach of
screen
pressing Ctrl+ Aand then Ctrl+ D.You can then close the SSH session.
I would recommend you to use
tmux
( terminal multiple x er ), it is more modern and has some advantages overscreen
.Advantage
According to the FAQ :
tmux
it has two objects, window and panel;screen
it only has windows.Use
Note that you must run
tmux
on the server you connect to via SSH, in the examples I'm assuming you've already connected.By default, it
tmux
uses the Ctrl+ combination bas a shortcut to execute the commands, but for the examples I will use the commands without shortcuts.It is preferable to name your sessions to maintain order, with the following simple example you can create a new session:
Well, now you have a session that you could return to even after logging out and find everything just the way you left it, let's test this by running a simple command:
To exit the session (detach):
And you should be back to the beginning where you created the session:
You can see the active sessions:
To re-enter the session (attach):
And now you should find things as you left them last time (including the command
tmux detach
):If you want to end the session (kill):
There should no longer be sessions:
Well, these are some basic commands that show the benefits of
tmux
, although as I mentioned above it is even faster to use the commands using shortcuts with Control+ b.References
I get a command to continue executing by adding an & to the end.
I think it's like telling it to run in the background and not run in the ssh shell
For example java -jar xxx.jar &