When you run a long script, compile a massive application, or start a heavy download in the Ubuntu terminal, that specific process is directly tied to your current terminal session window. If you accidentally close the terminal window, or if your SSH connection drops, Ubuntu will immediately terminate all the background processes attached to that session, ruining your work.
To prevent this, you can use the disown command. This command detaches a running background process from your specific terminal window and hands it over to the core operating system, allowing it to continue running even if you completely exit the terminal or log off.
How to Use the Disown Command
To use disown, the process you want to protect must be running in the background of your current terminal session.
Step 1: Start the command in the background
Normally, when you run a command, it occupies the terminal until it finishes. To start it in the background instead, simply add an ampersand (&) to the end of your command. For example:
python3 long_script.py &
The terminal will return a job number (e.g., [1]) and the Process ID (PID) of the task, and then give you back your command prompt.
Step 2: Detach the process
Now that the script is running in the background, you must detach it from the terminal window so it won’t die when the window closes.
Simply type:
disown
…and press Enter.
By default, typing disown on its own will automatically detach the most recently started background job. You will receive no visual confirmation; it will simply return a blank prompt.
You can now safely click the “X” to close your Ubuntu terminal window, or type exit to end your SSH session. Your Python script (or whatever command you launched) will continue running silently in the background of the server until it naturally finishes its task.