How to Run Linux Commands in Background and Detach in Terminal

Introduction

If you are a Linux user, you may have come across a situation where you need to run a command in the background and detach it from the terminal. This can be useful if you want to run a command that takes a long time to complete, or if you want to run a command without having to keep the terminal window open. In this tutorial, we will discuss how to run Linux commands in the background and detach them from the terminal. We will also discuss how to check the status of a background process and how to bring it back to the foreground.

How to Run Linux Commands in Background and Detach in Terminal

1. To run a Linux command in the background, you can use the “&” symbol at the end of the command. For example, to run the command “sleep 10” in the background, you would type “sleep 10 &”.

2. To detach a process from the terminal, you can use the “disown” command. For example, to detach the process “sleep 10” from the terminal, you would type “disown sleep 10”.

In this guide, we shall bring to light a simple yet important concept in process handling in a Linux system, which is how to completely detach a process from its controlling terminal.

When a Linux process is associated with a terminal, two problems might occur:

  1. Your controlling terminal is filled with so much output data and error/diagnostic messages.
  2. In the event that the terminal is closed, the process together with its child processes will be terminated.

To deal with these two issues, you need to totally detach a process from a controlling terminal. Before we actually move to solve the problem, let us briefly cover how to run processes in the background in Linux.

Run Linux Command or Process in Background

If a process is already in execution, such as the tar command example below, simply press Ctrl+Z to stop it then enter the command bg to continue with its execution in the background as a job.

You can view all your background jobs by typing jobs. However, its stdin, stdout, and stderr are still joined to the terminal.

$ tar -czf home.tar.gz .
$ bg
$ jobs
Run Linux Command in Background
Run Linux Command in Background

You can as well run a process directly from the background using the ampersand, & sign.

$ tar -czf home.tar.gz . &
$ jobs
Start Linux Process in Background
Start Linux Process in Background

Take a look at the example below, although the tar command was started as a background job, an error message was still sent to the terminal meaning the process is still connected to the controlling terminal.

$ tar -czf home.tar.gz . &
$ jobs

tar: ./.config/etcher: Cannot open: Permission denied
Linux Process Running in Background Message
Linux Process Running in Background Message

Keep Linux Processes Running After Closing Terminal

We will use the disown command, which is used after the process has been executed and put in the background, its work is to remove a shell job from the shell’s active list jobs, therefore you will not use fg, bg commands on that particular job anymore.

In addition, when you close the controlling terminal, the job will not hang or send a SIGHUP to any child jobs.

Let’s take a look at the below example of using the diswon bash built-in function.

$ sudo rsync Templates/* /var/www/html/files/ &
$ jobs
$ disown  -h  %1
$ jobs
Keep Linux Process Running After Closing Terminal
Keep Linux Process Running After Closing Terminal

You can also use the nohup command, which also enables a process to continue running in the background when a user exits a shell.

$ nohup tar -czf iso.tar.gz Templates/* &
$ jobs
Put Linux Process in Background After Closing Shell
Put Linux Process in Background After Closing Shell

Detach Linux Processes in the Terminal

Therefore, to completely detach a process from a controlling terminal, use the command format below, this is more effective for graphical user interface (GUI) applications such as Firefox:

$ firefox </dev/null &>/dev/null &

In Linux, /dev/null is a special device file that writes off (gets rid of) all data written to it, in the command above, input is read from, and output is sent to /dev/null.

As a concluding remark, provided a process is connected to a controlling terminal, as a user, you will see several output lines of the process data as well as error messages on your terminal. Again, when you close the controlling terminal, your process and child processes will be terminated.

Importantly, for any questions or remarks on the subject, reach us by using the comment form below.

If You Appreciate What We Do Here On Jassweb, You Should Consider:

Jassweb is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit Jassweb! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

How to Run Linux Commands in Background and Detach in Terminal

Linux is a powerful operating system that allows users to run commands in the background and detach from the terminal. This is useful for running long-running commands without having to keep the terminal window open. In this article, we will explain how to run Linux commands in the background and detach from the terminal.

Using the & Symbol

The simplest way to run a command in the background is to append an ampersand (&) to the end of the command. This will cause the command to run in the background and the terminal will immediately return to the prompt.

For example, to run the command “sleep 10” in the background, you would type:

sleep 10 &

The command will run in the background and the terminal will return to the prompt. You can check the status of the command by running the “jobs” command.

Using the nohup Command

The nohup command is a utility that allows you to run a command in the background even after you log out of the terminal. To use the nohup command, simply prepend the command with “nohup”.

For example, to run the command “sleep 10” in the background even after you log out, you would type:

nohup sleep 10

The command will run in the background and the terminal will return to the prompt. You can check the status of the command by running the “jobs” command.

Using the screen Command

The screen command is a utility that allows you to run a command in the background and detach from the terminal. To use the screen command, simply prepend the command with “screen”.

For example, to run the command “sleep 10” in the background and detach from the terminal, you would type:

screen sleep 10

The command will run in the background and the terminal will return to the prompt. You can check the status of the command by running the “jobs” command. To reattach to the command, simply run the “screen -r” command.

Conclusion

In this article, we explained how to run Linux commands in the background and detach from the terminal. We showed how to use the & symbol, the nohup command, and the screen command to run commands in the background. We hope you found this article helpful.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
Jassweb, Rai Chak, Punjab, India. 143518