- Unix Commands Reference
- Unix Commands - Home
disown Command in Linux
The Linux disown command removes jobs from the shell's job table. Note that it only removes the job from the jobs table, it does not stop the process.
In a shell, any running process inherits stdin, stdout, and stderr from the terminal. They are connected to the terminal. No other operations can be run until the long-running process finishes. So, to prevent it, the process can be sent to the background.
The background process still inherits the stdout and stderr. Sending processes to the background creates a jobs table. To remove multiple processes from the table, the disown command is used.
Table of Contents
Here is a comprehensive guide to the options available with the disown command −
- Syntax of disown Command
- disown Command Options
- Understanding the Jobs Table
- Examples of disown in Linux
Syntax of disown Command
The syntax for using the Linux disown command is as follows −
disown [options] [jobspec | pid]
To modify the disown command behavior, different options are used in the [options] field in the above syntax. While the [jobspec | pid] field takes the job ID (%1, %2, %3) and indicates the process ID to disown it.
disown Command Options
Options to use with the disown command are listed below −
Options | Description |
---|---|
-a | Remove all the jobs from the table |
-h | To mark a job by jobspec not to receive SIGHUP when the terminal session receives SIGHUP |
-r | It closes all the running jobs |
Understanding the Jobs Table
To manage the jobs first, we need to create a jobs table. The jobs table is a list of jobs that are running in the background.
To send a process to the background, an ampersand (&) sign is used. For example, in the following example the ping command’s standard output is redirected to the /dev/null, and & is signifying that the process should run in the background −
ping google.com > /dev/null &
In Linux, the /dev/null is a file that discards all the data written to it.
Another way of sending the process to the background is using the bg command. For instance, in the following example, the sleep command is running for 50 seconds.
sleep 50
No other tasks can be performed until this process completes. To send this process to the background first suspend it using ctrl+Z. This shortcut key suspends the process and sends it to the background as shown in the output.
To resume the background process, use the bg command −
The background process is resumed. To resume a specific process, specify the job ID (%1, %2, %3 ..) with the command.
To bring the running process to the foreground, use the fg command −
To list the background processes, the jobs command is used −
The jobs command prints the status of jobs in the current session. To print the jobs with PIDs use the following option −
jobs -l
The "+" sign with the job ID indicates the most recently added job in the jobs table. So, using the fg this job will be brought to the foreground. On the other hand, the "–" sign indicates the second most recently added job. If the + signed job is stopped or removed then this job with "–" will get the next "+" sign.
Here is a quick summary of the above section −
Commands & Shortcuts | Description |
---|---|
jobs | Displays the jobs in the current session |
bg | Resumes the suspended process |
fg | Brings the background process to the foreground |
Ctrl+Z | Suspends a running process |
Ctrl+C | Kills the currently running foreground process |
Examples of disown Command in Linux
This section focuses on the disown command usage with different options in Linux.
The first step is creating some background jobs. In the following example, three background processes are created −
./script.sh > /dev/null & ping google.com > /dev/null & sleep 80 &
The first job is a bash script that runs indefinitely and prints a current date and time.
Here is the script −
#!/bin/bash while true do sleep 3 echo "The date is $(date)" done
The second process is the ping command to ping a server indefinitely. The output of the script and the ping command is redirected to /dev/null. The third process is a simple sleep command running for 80 seconds. All the processes have been moved to the background using & sign.
In the following examples, these background jobs will be utilized.
Disown a Current Job
To remove a current job from the jobs table, run the disown command without any option.
disown
List the jobs using the jobs command −
It can be seen the most recently added job is removed.
As discussed earlier the disown command does not kill the process it just removes it. To verify it run the following command −
ps -ef | grep <process_name>
The output shows the process is still running. It will continue to run as long as the current session is not terminated.
Disown a Specific Job
To disown a specific job from the jobs table, specify the job ID with the disown command. The job ID can be obtained through the jobs command.
To disown the second job, run −
disown %2
In the output image, it can be seen that the second process is removed.
Disown a Job by Process ID (PID)
The job can also be disowned using PID. The job PIDs can be obtained using the jobs command with the -l option −
jobs -l
To disown a job with a PID, execute −
disown 20526
The output shows that the process with the specified PID is removed.
Disown All Jobs from the Jobs Table
To remove all the jobs from the jobs table, the -a option is used with the disown command −
disown -a
Use the jobs command to verify it.
To keep the Process Running after Logout
The disown command can also mark a process to keep it running even when the terminal session is closed. To mark the process -h option is used. For example, from the three background processes in the following example, to mark the third process, use job ID %3.
disown -h %3
The third process is marked.
Now, open a new terminal session and check the process −
ps -ef | grep <process_name>
It can be seen the process is running in pseudo terminal 6.
Conclusion
The disown command in Linux removes the job from the jobs table. It does not send the SIGHUP to the process. It just removes it from the jobs list. So, the process will continue to run as long as the session is running. To keep the process running even after closing the session, the -h option is used with disown command.
This tutorial covered the syntax of the disown command, jobs table, and usage using different examples.