How to Use Disown Command in Linux
The disown command in Unix-like systems, such as Linux, is used to remove a job from the shell’s job table, effectively disassociating it from the shell. This is useful for keeping long-running background processes alive even after you close the terminal session. Here’s a tutorial explaining how to use it:
What is disown?
When you run a command in the background using &, it becomes a background job. However, if you close your terminal, the background job will be terminated because it is associated with the terminal session. The disown command allows you to remove a job from the shell’s job table, preventing the job from being terminated when the terminal closes.
Common Use Cases
- Keep a process running after closing the terminal.
- Disassociate a process from the current shell session.
- Prevent a job from being affected by the
SIGHUPsignal when the terminal is closed.
disown Command Syntax
The basic syntax for the disown command is:
1
disown [options] jobID1 jobID2 ... jobIDN
Steps to Use disown
1. Start a background process
First, you need to run a command in the background. You can do this by appending & at the end of the command:
1
2
$ sleep 1000 &
[1] 12345
This command runs the sleep 1000 command in the background and outputs a job number ([1]) and the process ID (12345).
2. View the background jobs
To see a list of your current jobs, use the jobs command:
1
2
$ jobs -l
[1]+ 12345 Running sleep 1000 &
3. Disown the job
Now, if you want to disown this job (i.e., remove it from the shell’s job table), use the disown command with the job number:
1
$ disown %1
Here, %1 refers to job number 1. After running this command, the job will no longer appear in the output of jobs, and it won’t be terminated if you close the terminal.
4. Verify the job is disowned
If you run jobs after disowning a job, you will not see it listed anymore:
1
$ jobs -l
The job is no longer in the job table, meaning it will continue running even if you close the terminal.
5. Disown all jobs
If you want to disown all background jobs at once, use the following command:
1
$ disown -a
This will remove all jobs from the shell’s job table.
Using the
disowncommand without any options or job IDs removes the last job on the job table.
Advanced Usage
- Prevent a job from receiving
SIGHUP: Normally, when you close a terminal, processes that are tied to that session will receive aSIGHUPsignal. By disowning a job, you prevent this signal from being sent, ensuring that the job keeps running.