disown Command in Linux

Introduction

In this hands-on lab, delve into the power of the Linux disown command and discover how it enables you to detach processes from your shell session. This is crucial for ensuring processes keep running even after you disconnect or close your terminal. This tutorial will guide you through understanding the purpose of disown, detaching background processes, and effectively managing the output of multiple disowned tasks. Master these techniques to seamlessly execute long-running operations like system backups or hosting web servers without needing persistent terminal connections. Ideal for any aspiring systemadmin.

Understand the Purpose of the disown Command

This section explores the core function of the disown command within the Linux environment. Essentially, disown severs the link between a running process and the shell that initiated it, guaranteeing its continued operation even after the shell session terminates.

Normally, a process started in the foreground is tied to the lifespan of its parent shell. Closing the shell results in the process being terminated. However, employing disown allows you to untether the process, allowing it to run independently in the background, unaffected by shell closures.

This capability is invaluable when dealing with lengthy tasks, such as executing backup scripts or maintaining a web server. Using disown makes sure these processes persist even if you log off or shut down your terminal.

Let's begin by starting a simple background process and then employing the disown command to detach it from the active shell.

## Start a background process that runs indefinitely
$ sleep 1000 &
[1] 12345

## Detach the process from the shell using the disown command
$ disown %1

Example output:

In this example, we initiate a background process using sleep 1000 &, which instructs the sleep command to run for 1000 seconds (approximately 16 minutes) in the background.

Subsequently, we use the disown %1 command to sever the process's connection to the shell. The identifier %1 refers to the job number assigned to the background process, obtainable through the jobs command.

Post execution of the disown command, the process remains active in the background, impervious to logouts or terminal closures.

Disown a Running Process in the Background

This step teaches you to detach a currently executing background process using disown. This ensures that the process continues to run uninterrupted, even after you disconnect or close your terminal window, a critical skill for any systemadmin.

To begin, let's launch a background process that will run for an extended period:

## Start a background process that runs indefinitely
$ sleep 1000 &
[1] 12345

Next, utilize the disown command to detach this process:

## Disown the running process
$ disown %1

Example output:

As demonstrated, we begin by executing sleep 1000 &, starting a sleep command that runs for 1000 seconds (roughly 16 minutes) in the background.

We then use disown %1 to detach the process from the shell. Recall that %1 indicates the job number of the background process, which you can ascertain using the jobs command.

Once the disown command is executed, the process persists in the background, irrespective of whether you log out or close the terminal.

To validate that the process is still actively running, you can employ the ps command:

## Check if the process is still running
$ ps -ef | grep 'sleep 1000'
labex 12345 12321 0 11:30 pts/0 00:00:00 sleep 1000

The output confirms that the sleep 1000 process is still active, even after being disowned, illustrating the power of this command for Linux systemadmin tasks.

Disown Multiple Processes and Manage Their Output

This section demonstrates how to manage multiple background processes simultaneously using disown, including strategies for handling their output effectively, a common scenario for Linux systemadmin tasks.

First, let's start two separate background processes that run indefinitely:

## Start two background processes
$ sleep 1000 &
[1] 12345
$ sleep 2000 &
[2] 12346

Now, let's detach both processes using the disown command:

## Disown the running processes
$ disown %1 %2

Example output:

Here, we start two background processes using sleep 1000 & and sleep 2000 &. These commands will execute the sleep function in the background for 1000 and 2000 seconds, respectively.

The disown %1 %2 command then detaches both of these processes from the shell. The identifiers %1 and %2 represent the job numbers assigned to each background process, as shown by the jobs command.

After executing disown, both processes will continue to run independently in the background, unaffected by logouts or terminal closures.

To manage the output generated by these disowned processes, you can redirect it to separate files:

## Start two background processes and redirect their output to files
$ sleep 1000 > process1.log 2>&1 &
[1] 12345
$ sleep 2000 > process2.log 2>&1 &
[2] 12346

## Disown the running processes
$ disown %1 %2

In this expanded example, we start the two background processes and redirect their standard output (>) and standard error (2>&1) to process1.log and process2.log files, respectively. This ensures that all output from these processes is captured. We then use disown to detach these processes from the shell.

Once the processes have completed, you can examine the contents of process1.log and process2.log to review the output generated during their execution. This output redirection and the use of disown are key skills for a Linux systemadmin.

Summary

In this practical lab, you've learned how to leverage the disown command within Linux to effectively detach processes from your active shell session. This ensures that these processes continue running seamlessly in the background, even when you log out or close your terminal. You’ve practiced starting a background process and detaching it with disown, guaranteeing its continued operation. Furthermore, you've explored how to disown multiple processes and manage their respective outputs. The disown command is an invaluable tool for running long-term tasks, such as backups or web servers, without requiring an open terminal session. Mastering this command is essential for any aspiring Linux systemadmin seeking to efficiently manage background processes.

400+ Linux Commands