Shell Scripting - Disown Command
Last Updated :
20 Mar, 2025
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.
Also Read: How to Create a Shell Script in linux
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.
Conclusion
The disown
command in shell scripting is an tool for managing the background processes in Linux and UNIX-based systems. It allows users to detach running jobs from the shell session, ensuring they continue even after the user logs out. This is particularly useful for long-running commands, system monitoring, or server-side operations.
Similar Reads
Shell Scripting - Logout Command
The Linux logout command is a quick utility that allows you to log out of your present terminal session. It's similar to the action of clicking "Log Out" on the desktop of a computer, but for the terminal. It's particularly handy if you're using a login shellâa special terminal session associated wi
4 min read
Shell Scripting - Set Command
The `set` command in shell scripting is a powerful tool that used for controlling the behavior of the shell and the environment in which scripts run. It allows the users to modify the shell options and positional parameters which facilitates providing greater control over script execution and debugg
6 min read
Shell Scripting - Talk Command
The use of Shell scripting can simplify repetitive tasks and enable more complex operations with minimal code. Shell commands vary in syntax, with "talk" being a command to facilitate communication among users within the same network. Proper knowledge of the shell, OS, and available commands is nece
6 min read
Shell Scripting - True Command
A shell provides an interface with the help of which users can interact with the system easily. To directly interact with a shell, we use an operating system. On a Unix-based operating system, every time we write a command using a terminal, we interact with the system. To interpret or analyze Unix c
3 min read
Shell Scripting - Shopt Command
Unlike many shell commands, shopt is a bash-only shell command. It is a built-in BASH shell command that controls certain options for the shell session. One can consider this as a boolean ON/OFF switch for certain shell options. The âshoptâ command provides control over many settings that are used t
7 min read
Shell Scripting - Bash Trap Command
The trap command in Bash executes specific code in response to the signal given to the program. It traps the signal received by the program from the OS or the user and then executes the specific code provided by the programmer. It acts like a trigger in response to a signal. Just like try-catch stat
5 min read
Shell Scripting - Define #!/bin/bash
A shell provides an interface to connect with the system. When we use an operating system, we indirectly interact with the shell. While using a terminal every time on any Linux distribution system, we interact with the shell. The main function of the shell is to interpret or analyze Unix commands. A
3 min read
Introduction to Linux Shell and Shell Scripting
If we are using any major operating system, we are indirectly interacting with the shell. While running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by using the terminal. In this article we will discuss Linux shells and shell scripting so before understandi
8 min read
Linux Security Command Cheat Sheet
Maintaining a secure and hardened Linux system is crucial in today's threat-laden digital landscape. This comprehensive Linux Security Command Cheat Sheet serves as an invaluable resource for system administrators and security professionals, providing a concise reference to essential commands spanni
7 min read
Shell Script to Demonstrate Wait Command in Linux
Wait command is one of the process management commands. There are different process commands in Linux mainly 5 commands are widely used which are ps, wait, sleep, kill, exit. ps is an acronym for process status. It displays information about the active processes. wait command will suspend execution
4 min read