Detach running processes from SSH shells
After starting some process in a SSH shell, you’ll begin noticing it takes A LOT longer than anticipated. You want to close the session, but forgot to execute the process within a screen or tmux session. Silly you.
But there’s a solution - suspend the process, put it in the background, and disown it from the current shell. More info about bash’s job control.
To suspend a running process, press Ctrl + Z:
user@remotehost:~# rsync -av /backup/ ~/restore sending incremental file list file1 file2 ...
Ctrl + Z
+ Stopped rsync -av /backup/ ~/restore
Now put the process in the background:
user@remotehost:~# bg + rsync -av /backup/ ~/restore
And disown the job, with the job ID given by bg:
user@remotehost:~# disown -h %1
That’s it. Your process has been disassociated from it’s session, and will continue after logging out & closing the shell. There’s one downside, though: stdin and stdout will be redirected to /dev/null, manual double checking after your process finished is recommended. Or use screen next time.