microsoft / terminal

The new Windows Terminal and the original Windows console host, all in the same place!
MIT License
95.96k stars 8.35k forks source link

nohup process terminates when terminal disconnects from ssh session #17999

Open siddhsql opened 1 month ago

siddhsql commented 1 month ago

Windows Terminal version

1.20.11781.0

Windows build number

10.0.22631.4169

Other Software

No response

Steps to reproduce

  1. ssh to a remote machine from the Windows terminal
  2. 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

DHowett commented 1 month ago

Are you using ssh from within WSL, or the native Windows build of ssh?

siddhsql commented 1 month ago

I have tried both.

  1. ssh from Windows (no WSL)
  2. ssh from WSL using /usr/bin/ssh
lhecker commented 1 month ago

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.

siddhsql commented 1 month ago

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?

lhecker commented 1 month ago

I disabled the network adapter. I hoped that this would be similar to what you experienced.