terminal
terminal copied to clipboard
nohup process terminates when terminal disconnects from ssh session
Windows Terminal version
1.20.11781.0
Windows build number
10.0.22631.4169
Other Software
No response
Steps to reproduce
- ssh to a remote machine from the Windows terminal
- run a program (e.g., a node process that spins up a server listening to connections) like
nohup npm run &
leave terminal connected. after some period of inactivity (could be few hours) terminal will disconnect (broken pipe) as machine could go to sleep etc.
When this happens the program you started on the remote machine also terminates.
Expected Behavior
program you started on the remote machine should keep running.
Actual Behavior
program you started on the remote machine terminates. Note this does not happen if you exit from the ssh session by typing the exit command vs. leaving the terminal connected and it disconnects due to inactivity. if it helps, on Mac (with iTerm), the problem does not happen when leaving the terminal connected and it disconnects due to inactivity
Are you using ssh from within WSL, or the native Windows build of ssh?
I have tried both.
- ssh from Windows (no WSL)
- ssh from WSL using
/usr/bin/ssh
I just tried this with my own Linux server from Windows 11 24H2. After disconnecting the network, the process kept running in the background while SSH had exited.
What I suspect is happening is that your server is receiving a signal other than SIGHUP. nohup only catches that one signal and any other signal will still cause node to exit.
As an immediate workaround, I'd suggest either changing your command to npm run &; disown or to use a multiplexer like tmux or zellij. With tmux for instance, you first launch it by running tmux, then you execute your command npm run. No matter what happens now, that tmux and npm instance will keep running forever. You can explicitly detach from it by pressing Ctrl+B, D. You can reattach again by running tmux attach. zellij works similar but is a lot more beginner friendly.
Thanks Leonard. re: this:
After disconnecting the network, the process kept running in the background while SSH had exited.
could you be specific how you disconnected from the network? If I disconnect by explicitly exiting from the ssh session using the exit command, the process does keep running. I encounter the issue when the session terminates/disconnects because of prolonged inactivity (Broken Pipe). Did you repro that?
I disabled the network adapter. I hoped that this would be similar to what you experienced.