We’ve all experienced it: you’ve SSH-ed into a server and started a long-running task. Unexpectedly, it’s taking much longer than anticipated, and now you can’t close your laptop without interrupting the process. Fortunately, there’s a simple solution to close the terminal window without killing the ongoing task.
Quick and Easy Method
Follow these steps to keep your process running even after closing the terminal:
- Press
Ctrl + Zto suspend the current process - Type
bgand press Enter to resume the process in the background - Execute
disown -ahto remove all jobs from the shell and make them ignore SIGHUP - Type
exitto safely close the terminal
Demonstration
To experiment with this technique, try the following:
In Terminal 1:
bash -c "sleep 10" & disown -ah && exitOpen a new terminal (Terminal 2) and check if the process is still running:
ps aux | grep sleepYou’ll see that sleep is still in the process list. After 10 seconds, it will complete as expected.
This method allows you to maintain server processes even when disconnecting from SSH, ensuring your long-running tasks continue uninterrupted.
