34

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

46

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.

  • 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
  • [root@server ~]# bg -bash: bg: current: no such job – Muhammad Dyas Yaskur Feb 4 at 9:21
  • Why I got bash: bg: current: no such job ? – Muhammad Dyas Yaskur Feb 4 at 9:21
10

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.

  • 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
6

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.

  • 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
4

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.

2

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.

  • 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
1

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

nohup -p PID
  • nohup: invalid option -- 'p'. What version of nohup are you running on what kind of system? – Anthon Feb 18 '16 at 6:07
  • I mostly use Solaris and AIX. For Linux, you should check into the disown command. – tony Feb 18 '16 at 16:50
  • Yeah, there's no -p option on Linux , so this answer is Solaris and AIX specific. Good to know though – Sergiy Kolodyazhnyy Aug 7 '16 at 18:55
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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