38

sometimes I run an app in the gnome-terminal, but then I suddenly have to restart gnome or something. I guess the answer to the question is also useful then I want to disconnect from SSH where something is happenning.

Gnome's terminal tree looks like this:

gnome-terminal
    bash
        some-boring-process

Can I 'detach' bash from gnome-terminal (or detach some-boring-process from bash and redirect its output somewhere)? If I just kill gnome-terminal, bash will be killed to will all its subprocesses

mattdm
  • 40,245
valya
  • 635

7 Answers7

51

If some-boring-process is running in your current bash session:

  1. halt it with ctrl-z to give you the bash prompt
  2. put it in the background with bg
  3. note the job number, or use the jobs command
  4. detach the process from this bash session with disown -h %1 (substitute the actual job number there).

That doesn't do anything to redirect the output -- you have to think of that when you launch your boring process. [Edit] There seems to be a way to redirect it https://gist.github.com/782263

But seriously, look into screen. I have shells on a remote server that have been running for months.


Looks like this:

$ sleep 999999
^Z
[1]+  Stopped                 sleep 999999
$ bg
[1]+ sleep 999999 &
$ disown -h %1
glenn jackman
  • 85,964
  • Thank you. I selected your answer because it was the only one that answers the question (it was about already running process). I'm already using screen sometimes, though it has its issues and I don't want to use it for every bash session in my life – valya Mar 02 '11 at 16:58
  • You don't use it for every bash session, but for every remote machine where you spend a lot of time. – glenn jackman Mar 02 '11 at 17:33
  • I took the liberty of adding information about redirecting the output, please review it! – valya Mar 04 '11 at 18:05
  • [root@server ~]# bg -bash: bg: current: no such job – Muhammad Dyas Yaskur Feb 04 '19 at 09:21
  • Why I got bash: bg: current: no such job ? – Muhammad Dyas Yaskur Feb 04 '19 at 09:21
  • that's awesome maybe you could provide an actual example? my current best guess would be something like echo foo bg disown ctrl z but that seems wrong – Alexander Mills Apr 17 '20 at 20:04
  • @AlexanderMills, I added a shell session so you can see what it looks like. Hope that helps. You can see "sleep" running in the foreground, blocking my interactive shell. I hit Ctrl-Z, put it in the background and then disowned it. – glenn jackman Apr 17 '20 at 20:12
  • thanks, in this case I am looking to issue 1 command from the shell, and the rest be done automatically without more interaction from me – Alexander Mills Apr 17 '20 at 20:24
  • That's not what this question is asking. – glenn jackman Apr 17 '20 at 20:29
9

This is exactly what screen and tmux were created for. You run the shell inside the screen/tmux session, and you can disconnect/reconnect at will. You can also have multiple shell sessions running inside one gnome-terminal.

jsbillings
  • 24,406
  • Thanks, but, as I commented to the accepted answer: 1. the question was about already running process. 2. screen has its issues and I don't feel like using it for every bash session in my life – valya Mar 02 '11 at 16:59
5

screen, tmux, or dtach (possibly with dvtm) are all great for this, but if it's something where you didn't think to use one of those, you may be able to leverage nohup.

Hank Gay
  • 3,549
  • 1
    Thanks, but, as I commented to the accepted answer: 1. the question was about already running process. 2. screen has its issues and I don't feel like using it for every bash session in my life – valya Mar 02 '11 at 17:00
  • @valya Glad you found a solution. Just to be clear, though, nohup does work on a running process by using the -p option (if available). – Hank Gay Mar 02 '11 at 20:57
  • @Hank Gay: I can't see a -p option to nohup in the man pages; what system are you on? – Mikel Mar 02 '11 at 21:44
  • @Mikel Before I learned to stop worrying and love the tmux, I used the -p option on a Solaris box. I also have vague memories of doing it on a mongrel CentOS box (possibly zsh has a builtin that supports it?), but I can't find any docs on that. – Hank Gay Mar 02 '11 at 22:25
  • @Hank Gay: Neither bash nor zsh on my Ubuntu Linux system provide nohup. – Mikel Mar 02 '11 at 22:36
  • @Mikel that combined with my failure to find any docs makes me think I just imagined that. The Solaris thing was real, though. – Hank Gay Mar 02 '11 at 22:44
3

If you want to keep interacting with the child process rather than just backgrounding it and having it keep going, there's actually a program called retty which is a proof-of-concept for "stealing" a process from its current tty and reattaching it to the current one.

It, however, does some horrible things, including sticking some assembly code on to the stack of the re-attached application. And that code hasn't been updated for x86_64.

There's another program which takes a maybe-better approach, freezing the process in user-space to a file, from which it can later be restored (possibly on another tty.) This is cryopid, and that project too seems to have stopped in the proof-of-concept phase, and doesn't work with modern Linux as the code stands. Ah well.

Just thought this should be here for completeness. If you don't mind resorting to horrible voodoo, this is within the realm of possibility -- at least, theoretical possibility.

mattdm
  • 40,245
  • 1
    that reminds me of https://gist.github.com/782263 ... oh, that gist seems to answer my question, too – valya Mar 04 '11 at 18:01
1

If I fire something up which I want to finish no matter what (short of system reboot), I use nohup and run it in the background. Unlike screen and the like you can't reattach to the processs. However, baring redirection elsewhere any output can be found in nohup.out.

I do use screen when I want to be able to switch terminals for a process. Such as starting a process from home/work and switching to the other. Like any other terminal session output will eventual scroll off the top of the buffer.

EDIT: If you have already launched the process, you can disown the process to prevent the HUP signal from being sent when the process closes.

BillThor
  • 8,965
  • Thanks, but, as I commented to the accepted answer: 1. the question was about already running process. 2. screen has its issues and I don't feel like using it for every bash session in my life – valya Mar 02 '11 at 17:01
  • @valya I have seen references to disconnecting a running process from the program group. I don't know of any tools to do so though. The tool would likely need to redirect the std IO channels as well. I think I may have got it to work once, but decided nohup was simpler. – BillThor Mar 02 '11 at 17:51
0

You can make a process (PID) not receive a HUP signal when the terminal session is ended. Use the following command:

nohup -p PID
Anthon
  • 79,293
tony
  • 51
0

This script detaches child process from parent and assigns it to init process:

toDetach=$1

./$toDetach & # Runs the process - script on background

disown -h %$(jobs -l | grep $(ps -A | grep $toDetach | cut --delimiter=' ' -f1) | cut --delimiter=' ' -f1 | tr -d '[]+') # and then disowns it from parents process