Linux detach process from terminal

How to Start Linux Command in Background and Detach Process in Terminal

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

When a 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.

How to Start a Linux Process or Command 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, stderr are still joined to the terminal.

Run Linux Command in Background

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

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.

Linux Process Running in Background Message

Keep Linux Processes Running After Exiting Terminal

We will use disown command, it is used after the a process has been launched and put in the background, it’s 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 diswon bash built-in function.

Keep Linux Process Running After Closing Terminal

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

Put Linux Process in Background After Closing Shell

Detach a Linux Processes From Controlling 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:

In Linux, /dev/null is a special device file which 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 a controlling terminal, your process and child processes will be terminated.

Читайте также:  Fast switch user windows

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 TecMint, You Should Consider:

TecMint 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 TecMint! 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.

We are thankful for your never ending support.

Источник

How to Start Linux Command in Background and Detach Process in Terminal

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

When a 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.

How to Start a Linux Process or Command 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, stderr are still joined to the terminal.

Run Linux Command in Background

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

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.

Linux Process Running in Background Message

Keep Linux Processes Running After Exiting Terminal

We will use disown command, it is used after the a process has been launched and put in the background, it’s 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 diswon bash built-in function.

Keep Linux Process Running After Closing Terminal

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

Put Linux Process in Background After Closing Shell

Detach a Linux Processes From Controlling 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:

In Linux, /dev/null is a special device file which 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 a 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 TecMint, You Should Consider:

TecMint 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 TecMint! 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.

We are thankful for your never ending support.

Источник

how to detach process from the terminal in linux

In Linux and Unix systems, it is not uncommon to start a process from the command line or a terminal. In many cases, especially when logging remotely into a system without access to a graphical desktop, it is pretty much the only option.

One of the issues with starting the process from the terminal is that it starts as a child process of the shell itself. This means that when the terminal or shell is shut down or killed, it will also kill the running child processes. If you like a process that has been started from a terminal to remain running even after you have logged out and closed the terminal, then you need to detach the process from the terminal before closing the terminal.

Detaching or disassociating the process is where the child process is removed from the jobs table and disassociated from the parent process, such that closing of the parent shell will not kill the child process. There are a couple different options you have when detaching the child process.

disown command

If you are using the bash or zsh shell, then you can use the disown command to detach process. Using disown will remove the job from the system’s jobs table and make it such that it will not get SIGHUP or kill signals from the parent or the shell process. This makes it safe to log out of the shell later.

You have to execute the disown command from the same shell that the process is attached to. This means that if the process is active, then it need to be stopped first and pushed as a background job before you can disown it. Hit Ctrl-Z to stop the process and get the prompt. Then issue the command bg to restart the process in the background.

You can now disown the process by using either a process id, the job number in the current shell, or the job name.

where processid is the id the process that want detached from a shell.

bash$ disown %number

where %number refers to the job number, for example it could be %2 or %3. The jobs are processes running in the background in the current shell. You can view the current jobs by using the jobs command.

bash$ disown %string

where %string refers to jobs that have names that starts with what is specified as string.

You can also execute this command without any command line arguments, in which case the most recent background job is used as the default. When the parent shell is killed, the process is not sent a SIGHUP signal and the init becomes the parent process.

nohup command

If you already know at the time of starting up the process that you want to run the process without attaching it to the shell, then you can use the nohup command at startup. The nohup command is a posix utility that configures a command to ignore the SIGHUP signal.

Читайте также:  Trisquel gnu linux обзор

bash$ nohup firefox &

In the simplest form, the above example starts the firefox program in the background disassociated from the command shell. Although this works in most cases, when there is output being spitted out by the program, it could hang the terminal when attempting a logout. This could also be the case when you are remotely logged in using an ssh session.

bash$ nohup firefox > firefox-temp.out 2> firefox-temp.err

In order to prevent the hanging of terminals or sessions, you can redirect all three of the I/O streams of the program as shown above. Some versions of the nohup, not all though, has a -p option that accepts a process id, that allows a running process to be nohup-ed.

screen command

Another option is to use the screen command, although it is a little more involved than the previous methods. Basically, screen can be used to detach the process and attach it back at a later time if needed. The screen command also gives you the ability to start multiple programs if needed.

bash$ screen firefox

This will start the program firefox. You can now detach the screen from the terminal. In order to detach, from the screen window press Ctrl-A followed by d to detach the screen from the terminal. You can also detach the screen from another terminal, if you have access to one.

You can find the screens and their ids by using the ls command line option.

You can reattach the screen later, if needed by using the -r command line option with the screen id.

All of the above methods will allow you to run a process or program detached from the shell it was started from. If you have used disown, it is very hard to reattach to the terminal later if needed.

Источник

How to completely detach a process

I want to spawn a process and run it at the top level so that when the shell exits, the process keeps running.

I had mistakenly thought that nohup would do this, but when I exit from the shell (the shell is a lxc container and I ssh into that) and re-enter, the process is gone.

It seems (looking at pstree) that the process is still under the invoking shell even with nohup or disown.

I tried a few options and setsid seems to work — but am I doing something wrong with nohup here ?

success. But why did nohup not achieve the same? Is there a way to use nohup more correctly?

3 Answers 3

It’s a little hard to diagnose, as you don’t give source for myproc , but I suspect that your problem has something to do with «controlling TTY». I wrote a small shell script that just calls sleep 100 . I ran it under nohup :

If you look at the ps output, you see a column named «TTY». At least some versions of nohup (the one I used above is from GNU coreutils, version 5.97, so relatively old). When I exited the bash shell from which I started sleeper , the TTY column changed to ‘?’, meaning that sleeper didn’t have one.

If myproc doesn’t deliberately detach itself from it’s controlling TTY, it’s still possible to get things like a SIGPIPE signal if it tries to write to stdout. It seems to me other things are possible, but I can’t recall or google anything up.

If you can find or compile «daemonize», you might want to try it. If myproc is compiled, you could modify the source to call the daemon(3) library function.

Источник

Оцените статью