The disown
command is a built-in shell command in Linux that is used to remove the job from the current shell, which means that the job will no longer be associated with the terminal. This command is useful when you want to run a process in the background, but you don’t want it to be terminated when you close the terminal.
Overview
The disown
command is used to remove a job from the shell’s job control list. When you run a command in the terminal, it is considered a job. By default, jobs are associated with the terminal and are subject to the same lifecycle as the terminal. This means that if you close the terminal, the job will be terminated.
To use the disown
command, you need to first run a command in the background using the &
operator. For example, to run the sleep
command in the background, you can run the following command:
$ sleep 60 &
This will run the sleep
command for 60 seconds in the background. To remove this job from the shell’s job control list, you can use the disown
command:
$ disown %1
In this example, %1
refers to the first job in the job control list. You can also specify the job by its process ID (PID) using the -h
option:
$ disown -h 1234
This will remove the job with the PID of 1234 from the job control list.
Use cases
- Running long-running commands in the background without worrying about them being terminated when you close the terminal.
- Removing a job from the job control list so that it is not subject to the shell’s job control commands (e.g.
fg
,bg
,kill
).
Options
The disown
command has the following options:
Option | Description |
---|---|
-h |
Specifies that the job should be removed by its process ID (PID) instead of its job number. |
-ar |
Removes all jobs from the job control list. |
-a |
Removes all jobs from the job control list except for the currently running one. |
-r |
Removes all running jobs from the job control list. |
Troubleshooting tips
- If you are not sure which job to remove, you can use the
jobs
command to list all jobs in the job control list. - If you try to remove a job that has already completed, you will get an error message.
Notes
- The
disown
command only removes the job from the shell’s job control list. It does not terminate the job or prevent it from running. - If you want to run a command in the background and disown it at the same time, you can use the following syntax:
command & disown
.