How to use the command 'disown' (with examples)
- Linux
- December 25, 2023
The ‘disown’ command is used in the Unix shell to allow sub-processes to live beyond the shell that they are attached to. It is often used to remove jobs from the shell’s job control, allowing them to continue running even after the shell has been exited. This can be useful in scenarios where you want to disconnect a process from the current shell and leave it running in the background.
Use case 1: Disown the current job
Code:
disown
Motivation: Sometimes, after starting a long-running process in the foreground, you may realize that you forgot to run it in the background. In such cases, you can use the ‘disown’ command without any argument to remove the current job from the shell’s job control, allowing it to keep running even after you exit the shell.
Explanation: This use case of the ‘disown’ command doesn’t require any arguments. It simply removes the current job, which is the most recent background process executed in the shell, from the job control.
Example output:
$ sleep 600 &
[1] 12345
$ disown
$ exit
In this example, the ‘sleep 600’ command is started in the background. The background job is then disowned using the ‘disown’ command. Finally, the shell is exited. Even after the shell is exited, the ‘sleep 600’ process continues to run in the background.
Use case 2: Disown a specific job
Code:
disown %job_number
Motivation: When you have multiple jobs running in the background and you want to remove a specific job from the shell’s job control, you can use the ‘disown’ command with the job number. This allows the specified job to continue running even after the shell has been exited.
Explanation: In this use case, you need to provide the job number as an argument to the ‘disown’ command. The job number can be obtained by using the ‘jobs’ command. By specifying the job number, you can selectively remove the desired job from the shell’s job control.
Example output:
$ sleep 300 &
[1] 12345
$ sleep 400 &
[2] 23456
$ jobs
[1]- Running sleep 300 &
[2]+ Running sleep 400 &
$ disown %1
$ jobs
[2]+ Running sleep 400 &
$ exit
In this example, two background jobs are started with ‘sleep 300’ and ‘sleep 400’ commands. The job number for each job is obtained using the ‘jobs’ command. Then, the ‘disown’ command is used to remove the first job from the shell’s job control. After that, the ‘jobs’ command only displays the second job, indicating that the first job has been successfully disowned.
Use case 3: Disown all jobs
Code:
disown -a
Motivation: If you have multiple background jobs running in the shell and you want to remove all of them from the shell’s job control, you can use the ‘disown’ command with the ‘-a’ option. This allows all the background jobs to continue running even after the shell has been exited.
Explanation: The ‘-a’ option is used to disown all jobs. When this option is passed to the ‘disown’ command, it removes all jobs from the shell’s job control, allowing them to keep running in the background.
Example output:
$ sleep 500 &
[1] 12345
$ sleep 600 &
[2] 23456
$ jobs
[1]- Running sleep 500 &
[2]+ Running sleep 600 &
$ disown -a
$ jobs
$ exit
In this example, two background jobs are started with ‘sleep 500’ and ‘sleep 600’ commands. The ‘jobs’ command lists both the jobs. The ‘disown’ command with the ‘-a’ option is then used to remove all jobs from the shell’s job control. After that, the ‘jobs’ command doesn’t display any jobs, indicating that all the jobs have been successfully disowned.
Use case 4: Keep job, but mark it to not receive SIGHUP on shell exit
Code:
disown -h %job_number
Motivation: In some cases, you may want to keep a job running in the background but prevent it from receiving the SIGHUP (hangup signal) when the shell that started the job exits. This can be useful when you want to disconnect a job from the current shell, but also want to ensure that it continues running even when the shell is closed.
Explanation: To keep a job running in the background but mark it so that it doesn’t receive SIGHUP on shell exit, you can use the ‘disown’ command with the ‘-h’ option followed by the job number. This option causes the job to be “hung up” and prevents it from being terminated when the shell is closed.
Example output:
$ sleep 700 &
[1] 12345
$ disown -h %1
$ exit
In this example, a background job is started with the ‘sleep 700’ command. The job number is obtained using the ‘jobs’ command. Then, the ‘disown’ command with the ‘-h’ option is used to mark the job so that it doesn’t receive SIGHUP on shell exit. Finally, the shell is exited and the job continues to run.