Open In App

Shell Scripting – Disown Command

Improve
Improve
Like Article
Like
Save
Share
Report

Shell is a command language interpreter. It is used in most of the UNIX/LINUX-based distros. Shell support scripting means we can automate the Shell commands by writing some scripts with different logic, looping, and decision making. This benefits us to automate some time-consuming and tedious jobs. Here in this article, we are going to discuss the disown command within shell Scripting. Disown command helps remove jobs or keep them running after you log off from ssh and terminal session in the background.

Syntax of Disown Command:

disown [options] job1 job2 ... jobn

Let’s understand using examples:

Example 1: disown command for removing all jobs

Example Script:

# starts some jobs
cat /dev/random > /dev/null &
ping google.com > /dev/null &
jobs -l
echo "some jobs are started"

# disown command called to remove all the jobs
disown -a 
echo "all the jobs are removed"
jobs -l #displays all the jobs

Script Output:

[1]-  6174 Running                 cat /dev/random > /dev/null &

[2]+  6175 Running                 ping google.com > /dev/null &

some jobs are started

all the jobs are removed

Below is the terminal shell pictorial depiction after executing the following script:

 

Here in this example, the following script first starts some jobs in the background. Then using jobs -l it displays all the jobs in the terminal. Next, disown -a command is called which removes all the jobs from the current shell. At the end of the script, the jobs -l command is called by the script but it doesn’t print anything in the terminal means that now there are no jobs present in the current shell. 

Example 2: Disown command for removing only running jobs

Example with Output:

satyajit@satyajit-ThinkPad:~$ jobs -l

[1]-  8735 Running                 cat /dev/random > /dev/null &

[2]+  8736 Stopped (signal)        ping google.com > /dev/null

satyajit@satyajit-ThinkPad:~$ disown -r

satyajit@satyajit-ThinkPad:~$ jobs -l

[2]   8736 Stopped (signal)        ping google.com > /dev/null

Below is the terminal shell pictorial depiction after executing the following :

 

In the above example, we have initially 2 jobs – one is running & another one is stopped. Then we have used the disown -r command and it only removes the running task. So after running the disown command when we checked the available jobs, we found the stopped one till present there.

Example 3: Disown command to keep Jobs running after Log Out

If we close the terminal shell, then all the running jobs are automatically terminated but with the help of disown command, we can prevent that.

Example Script:

# starts some jobs
ping google.com > /dev/null &
jobs -l #displays all the jobs
disown -h %1 #disown command called 
exit

Script Output:

satyajit@satyajit-ThinkPad:~$ ./gfg.sh

[1]+  9935 Running                 ping google.com > /dev/null &

Below is the terminal shell pictorial depiction after executing the following :

 

In the above example, we have used disown command with the -h option which makes the job independent of the current shell so that it can be in the running state even after the exit or signing off from the shell.


Last Updated : 02 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads