disown

2024-07-11

Understanding the disown Command

The basic function of disown is simple: it removes a job from the shell’s job control list. This means the shell no longer tracks the process’s status or manages its termination. Even if you close the terminal or log out, the detached process will continue running.

Syntax

The general syntax of the disown command is:

disown [-h] [jobspec ...]

Practical Examples

Let’s look at some practical applications of disown with concrete examples.

Example 1: Disowning a Single Background Job

Suppose you’ve started a long-running process in the background:

sleep 600 &  # Sleep for 10 minutes in the background

You can then disown this job using its job ID:

disown %1

or using the -h flag for added protection:

disown -h %1

Example 2: Disowning Multiple Jobs

If you have multiple background jobs, you can disown them all at once:

sleep 300 &
sleep 600 &
ping google.com &

jobs  # List the background jobs
disown %1 %2 %3 # Disown all jobs

Example 3: Disowning using PIDs

You can disown a process using its Process ID (PID), obtained using the ps command:

long_running_process &
pid=$(jobs -p | awk '{print $1}') # Get the PID of the last background job
disown $pid

Example 4: Disowning a specific job using job number:

sleep 100 &
sleep 200 &
jobs




disown 1 #Disowns job number 1

These examples illustrate the flexibility and power of disown. Remember that once a process is disowned, you’ll lose the ability to manage it directly through the shell’s job control. However, you can always use other tools like ps and kill to monitor and interact with the process, even after disowning it. Using nohup in conjunction with disown can provide even greater robustness for processes that might be sensitive to terminal disconnections. However, understanding when and how to use disown is a skill for any Linux user.