×
DISOWN command linux

disown is a part of the Unix shells ksh, bash and zsh that is used to remove jobs from the job table. It is basically a shell builtin command very similar to cd, pwd etc. In some cases, you need to keep your program running longer even after you log out of the server. For example, compiling a large program may take a really long time. You can use one of the feature of bash shell called disown in that
case.

In this tutorial, we will show you how to remove jobs from the job table using the disown command.

Basic Syntax of disown command

The basic syntax of disown command is shown below:

disown [options] jobid1 jobid2 jobid3

Brief explanation of each option you can use with disown command is shown below:

-a : This option will remove all jobs if jobID is not provided.

-h : This option mark each jobid so that SIGHUP is not sent to the job if the shell receives a SIGHUP.

-r : This option will remove only running jobs.

Working with disown command

In this section, we will show you how to use disown command with some example.

First, you will need to start some background jobs in your system. You can start them with the following commands:

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

Now, run the following command to list the current jobs with jobid:

jobs -l

You should get the following output:


[1]- 9387 Running cat /dev/random > /dev/null &
[2]+ 9388 Running ping google.com > /dev/null &

Where:

[1] : jobid for process id 9387 for the command cat.

[2] : jobid for process id 9388 for the command ping.

Now, run the disown command without any option as shown below:

disown

This command will remove all the active jobs (denoted by + symbol) from the job table.

Now, run jobs -l command again:

jobs -l

You should get the following output:

[1]+ 9387 Running cat /dev/random > /dev/null &

You can see the sample outputs of all commands in the following screen:

If you want to remove all jobs, run the following command:

disown -a

You can also remove only running jobs by running the following command:

disown -r

Prevent running job from getting killed after exit from a shell

When you logout from your terminal, all running jobs will be killed automatically. To prevent this, you will need to run disown -h command before exit from the shell.

For example, let’s run the following command:

cat /dev/random > /dev/null &

Now, list the running jobs with the following command:

jobs -l

You should see that cat command is running in the background as shown below:


[1]+ Running cat /dev/random > /dev/null &

Next, run the disown command for your job to prevent it from getting killed on exit.

disown -h %1

Now, exit from the terminal with the following command:

exit

Conclusion

In the above tutorial, we learned how to remove jobs from the job table with disown command. We also learned how to keep jobs running after exit from the terminal.