11

Is there a way to reattach to a specific terminal a process that was started from terminal with &? For instance, I started Kate by:

kate &

and now I want to reattach the process to a given terminal.

2 Answers 2

10

There are two issues here:

1) Death by HUP and avoiding that:

If you start a program in a shell and then end that shell then all child processes will be killed. This includes programs running in to background. (Those which you start with an & at the end of the command, like the kate & in your example, but also processes which you suspended with controlz and then bg).

Both will die once you close the shell.*1

You can work around this by disowning the backgrounded program. It will then keep running, but you can no longer get it back to the foreground of a shell.

You can use nohup (e.g. nohup kate &) to keep the output from the program in a file. But full reattachment to a shell is also no longer possible.


2) Terminal emulators.

You can also start a program which emulates a terminal on its own and which is specially build so you can reconnect to it. screen and tmux are two examples of this.

Example of screen usage:

  • log in or ssh to host.
  • screen (starts screen. By default this starts a new shell).
  • kate (starts kate).
  • ControlAD (Detaches the screen, you will be back at the same prompt you had when you logged in for the first time.).
  • logout

Walk to another computer, or come back hours later. (or both :)) - log in or ssh to host. - screen -r (Reattach screen. you are now back at a shell where you are running kate.

Useful commands for screen:

screen -d
Detach a screen from outside the screen program.
Use ControlAD to detach from inside screen.

screen -r Reconnect to the screen (e.g. from a different location)

screen -dr
If there is an attached screen, then first detach it. Then attach it locally.
Useful of you forgot to detach it at the office and log in from home ;)

screen -x
Attach to a running screen without closing the other session. Useful for shared debugging, help session etc. etc.


*1: Edit: When I answered this I assumed that all shells would always sends a sigHUP to all of its children. However this is an option and depending on the shells it may be configurable. In some cases it even is no longer the default.

Example1: In bash you can configure this option via the buildin command shopt. (shopt -s huponexit).

Example2: With zsh you want setoptHUP.

2
  • 1
    fg solves my problem only if I didn't close the parent terminal. However, if I close the parent terminal I won't be able to reattach my process to it. Maybe I haven't explained well my question.
    – Mauren
    Sep 12, 2013 at 20:18
  • 9
    That is why you first start screen. You can then start the program (without using the & to background it. You can detach (sort of close) the screen with control-A-D and the program will keep running. The next morning you can log in and use screen -r to reattach and you will be back at the program. You can even log in from home (e.g. via SSH) and reattach the screen.
    – Hennes
    Sep 12, 2013 at 22:32
26

fg brings something from the background to the foreground in the current terminal.

3
  • 1
    But that won't work if I close the parent terminal.
    – Mauren
    Sep 12, 2013 at 20:16
  • @Mauren, ah, I didn't quite understand your question. Hennes has the correct answer. Look into using Tmux. Sep 12, 2013 at 20:33
  • I didn't know fg could do that. This worked for me with a simple backgrounded task by bg or prog &. Thanks.
    – The Matt
    Dec 9, 2018 at 16:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .