How to Use Disown Command in Linux

October 15, 2020

Introduction

The disown command is a part of the Unix ksh, bash, and zsh shells and is used to remove jobs from the current shell. Like cd or pwd, it is a shell built-in command, and doesn’t require root privileges.

This tutorial will cover different ways you can use the disown command in Linux to both terminate jobs and keep them running after you log off.

Disown command in Linux

Prerequisites

  • A system running Linux
  • Access to the command line / terminal window

disown Command Syntax

The basic syntax for the disown command is:

disown [options] jobID1 jobID2 ... jobIDN

Using the disown Command in Linux

The disown command in Linux is used to remove jobs from the job table. You can also use it to keep a longer and more complex job running in the background even after you log out of the server.

Review Ongoing Jobs

In order to use the disown command, you first need to have jobs running on your Linux system.

In this example, we will start up a couple of jobs running in the background:

cat /dev/random > /dev/null &
ping google.com > /dev/null &

Use the jobs command to list all current jobs:

jobs -l

You should get a similar output to the one seen below:

Review ongoing jobs


The ping command is denoted by ‘+’, which means it’s a currently active job.

The cat command is denoted by ‘-’, meaning it will become the active job if the ping command is terminated.

Remove All Jobs

To remove all jobs from the job table, use the following command:

disown -a
Remove all jobs using disown command in Linux

Remove Specific Jobs

If you want to remove a specific job from the job table, use the disown command with the appropriate job ID. The job ID is listed in brackets on the job table:

JobID in located on the left-hand side, in brackets

In our example, if we want to remove the ping command, we need to use the disown command on job 2:

disown %2
Removing a specific job using the disown command

Using the disown command without any options or job IDs removes the last job on the job table:

Using the disown command to remove the last job on the job table

Note: Job IDs always begin with the % character. To select a specific job from the list, use %n, where n is the job number. Using %% selects the currently active job.

Remove Currently Running Jobs

To remove only the jobs currently running, use the following command:

disown -r

In our example, the above-mentioned command clears the job table, since both jobs are currently running in the background:

Removing currently active jobs with the disown command

Keep Jobs Running After You Log Out

Once you exit your system’s terminal, all currently running jobs are automatically terminated. To prevent this, use the disown command with the -h option:

disown -h jobID

In our example, we want to keep the cat command running in the background. To prevent it from being terminated on exit, use the following command:

disown -h %1

After you use the disown command, close the terminal:

exit

Any jobs you used the disown -h command on will keep running.

Conclusion

After following this tutorial, you have learned to use the disown command to remove jobs from the job list or keep them running even after you close the terminal window.

For more Linux commands, take a look at our Linux Commands Cheat Sheet.

Was this article helpful?
YesNo
Aleksandar Kovačević
With a background in both design and writing, Aleksandar Kovacevic aims to bring a fresh perspective to writing for IT, making complicated concepts easy to understand and approach.
Next you should read
How to Use the hostname Command in Linux
October 8, 2020

The Linux hostname command lets you view your computers domain, hostname, and IP address. You can also use it...
Read more
Date Command in Linux: How to Set, Change, Format and Display Date
October 10, 2024

The date command is used to display and set the system date and time. Learn how to use the date command and...
Read more
How to Use the sudo Command in Linux
October 15, 2020

sudo stands for SuperUser DO, and it's used to temporarily elevate privileges in Linux. This guide will show...
Read more
19 Crucial Linux ls Commands to Know
October 15, 2020

The ls command (short for "list") lists information about directories and any type of files in the working...
Read more