Linux disown Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn about the Linux disown command and how to use it to detach running processes from the shell, allowing them to continue executing even after you log out of the system or close the terminal. The lab covers understanding the purpose of the disown command, detaching a running process in the background, and managing the output of multiple disowned processes. This knowledge can be useful for running long-running tasks, such as backup scripts or web servers, without the need to keep the terminal session open.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/ProcessManagementandControlGroup(["`Process Management and Control`"]) linux/ProcessManagementandControlGroup -.-> linux/jobs("`Job Managing`") linux/ProcessManagementandControlGroup -.-> linux/bg_running("`Background Running`") linux/ProcessManagementandControlGroup -.-> linux/bg_process("`Background Management`") subgraph Lab Skills linux/jobs -.-> lab-422643{{"`Linux disown Command with Practical Examples`"}} linux/bg_running -.-> lab-422643{{"`Linux disown Command with Practical Examples`"}} linux/bg_process -.-> lab-422643{{"`Linux disown Command with Practical Examples`"}} end

Understand the Purpose of the disown Command

In this step, you will learn about the purpose of the disown command in Linux. The disown command is used to detach a running process from the shell, allowing it to continue running even after the shell session has been closed.

When you start a process in the foreground, it is associated with the current shell session. If you close the shell, the process will be terminated. However, by using the disown command, you can detach the process from the shell, allowing it to continue running in the background, even after the shell has been closed.

This can be useful in scenarios where you need to run a long-running process, such as a backup script or a web server, and you want to ensure that the process continues to run even if you log out of the system or close the terminal.

Let's start by running a simple background process and then using the disown command to detach it from the 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 the example above, we first start a background process using the sleep 1000 & command. This will run the sleep command in the background for 1000 seconds (approximately 16 minutes).

Next, we use the disown %1 command to detach the process from the shell. The %1 refers to the job number of the background process, which you can find using the jobs command.

After running the disown command, the process will continue to run in the background, even if you log out of the system or close the terminal.

Disown a Running Process in the Background

In this step, you will learn how to disown a running process in the background, allowing it to continue executing even after you log out of the system or close the terminal.

First, let's start a background process that runs indefinitely:

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

Now, let's disown the running process using the disown command:

## Disown the running process
$ disown %1

Example output:

In the example above, we first start a background process using the sleep 1000 & command. This will run the sleep command in the background for 1000 seconds (approximately 16 minutes).

Next, we use the disown %1 command to detach the process from the shell. The %1 refers to the job number of the background process, which you can find using the jobs command.

After running the disown command, the process will continue to run in the background, even if you log out of the system or close the terminal.

To verify that the process is still running, you can use 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

As you can see, the sleep 1000 process is still running, even though we have disowned it.

Disown Multiple Processes and Manage Their Output

In this step, you will learn how to disown multiple processes running in the background and manage their output.

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

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

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

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

Example output:

In the example above, we first start two background processes using the sleep 1000 & and sleep 2000 & commands. This will run the sleep command in the background for 1000 and 2000 seconds, respectively.

Next, we use the disown %1 %2 command to detach both processes from the shell. The %1 and %2 refer to the job numbers of the background processes, which you can find using the jobs command.

After running the disown command, both processes will continue to run in the background, even if you log out of the system or close the terminal.

To manage the output of the disowned processes, you can redirect their output to a file:

## 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 the example above, we start the two background processes and redirect their output to process1.log and process2.log files, respectively. We then use the disown command to detach the processes from the shell.

After the processes have finished running, you can check the content of the log files to see the output of the disowned processes.

Summary

In this lab, you will learn how to use the disown command in Linux to detach running processes from the current shell session. This allows the processes to continue running in the background, even after you log out or close the terminal. You will start a background process, then use the disown command to detach it, ensuring it continues to run. Additionally, you will learn how to disown multiple processes and manage their output. The disown command is useful for running long-running tasks, such as backups or web servers, without the need to keep the terminal open.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like