Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question

5 Answers 5

up vote 21 down vote accepted

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.

share|improve this answer
    
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 2 '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 2 '11 at 17:33
    
I took the liberty of adding information about redirecting the output, please review it! –  valya Mar 4 '11 at 18:05

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.

share|improve this answer
    
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 2 '11 at 16:59

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.

share|improve this answer
    
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 2 '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 2 '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 2 '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 2 '11 at 22:25
    
@Hank Gay: Neither bash nor zsh on my Ubuntu Linux system provide nohup. –  Mikel Mar 2 '11 at 22:36

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.

share|improve this answer
    
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 2 '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 2 '11 at 17:51

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.

share|improve this answer
1  
that reminds me of gist.github.com/782263 ... oh, that gist seems to answer my question, too –  valya Mar 4 '11 at 18:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.