Remote code execution after tmux reboot
I've read about tmux-resurrect
, but it's still not clear to me if I can do the following:
- start
tmux
session -
ssh
to remote server - launch long-running code on remote host
- kill tmux server (e.g. local machine reboot)
- restore & reattach tmux session (e.g. using
tmux-resurrect
) - continue monitoring the code I launched on the remote server
Do I need to use nohup
in step 3? or can tmux-resurrect
(or some other tool) take care of it?
Solution 1:
No local-side tool can do this, because of what the server "sees":
- if you disconnect SSH gracefully, the server will notice this immediately;
- if you just disappear (e.g. by a hard reset), the connection will stay open on the server side until some kind of timeout (compare
ClientAlive*
options, TCP keepalive concept – see this answer for some details).
Even if you intended to use tmux-resurrect
or another tool on the local side, the server doesn't know, doesn't care. It will terminate your long-running code unless you used nohup
or better…
If you can, use tmux
(or screen
) on the server:
- Don't enter
tmux
session on the client. -
ssh
to the server. - Start
tmux
session on the server. - Launch long-running code there.
- Disconnect anyhow: gracefully or not, with or without detaching from the remote
tmux
. -
ssh
again. - Your
tmux
session is still there (unless something bad happened to the server in the meantime); reattach withtmux a
.
I use tmux
this way on daily basis and I think this is the right way. On my laptop tmux
sessions last for few hours at most, I shut it down every night; but I have access to a couple of servers where my tmux
sessions run for months. When I'm writing this, on one of them there's watch df -h
process running literally for a week in a tmux
session started like two months ago.