Saturday, November 16, 2024
Google search engine
HomeGuest BlogsHow to Use Disown Command in Linux

How to Use Disown Command in Linux

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 LinuxDisown 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 jobsReview 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 LinuxRemove 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 bracketsJobID 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 commandRemoving 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 tableUsing 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 commandRemoving 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

RELATED ARTICLES

Most Popular

Recent Comments