6

Possible Duplicate:
How can I close a terminal without killing the command running in it?

How to launch a GUI application (e.g. gedit) from terminal and detach it from there in one step?

  • How do you later reconnect? – Aaron D. Marasco Jul 23 '11 at 12:58
  • Don't clearly understand the question. Do you mean attach detached process back to the shell and make it a child process of the shell again? If so, why would I need it? – Anton Moiseev Jul 23 '11 at 13:11
  • I mean the point of detaching/disowning is to allow you to leave a program running when you log out. But if you disowned gedit and log out, what's the point? When you logged back in, how would you reconnect to use it again? – Aaron D. Marasco Jul 23 '11 at 13:16
  • @Aaron, My use case is different. I want to detach/disown a process just to launch it from shell, don't like search appropriate shortcut via GUI. At the same time I want to continue to use the same instance of shell for further tasks. Sometimes I can close a shell and don't want to lose my opened GUI applications. – Anton Moiseev Jul 23 '11 at 13:30
  • Got it... you're never killing X. – Aaron D. Marasco Jul 23 '11 at 13:32

3 Answers3

9

The & operator enables the application to run in the background. Use

nohup gedit

or

nohup gedit &

(the latter lets you use the terminal after launching gedit, just press return to send it to the background). Nohup dispatches the application completely from the terminal and session.

Caleb
  • 70,105
muffel
  • 2,818
  • 1
    As Caleb said below, a convenient shortcut to disown/background a process in zsh is &!. Combined with the >& shortcut to redirect STDOUT and STDERR, I've made a convenient alias: alias -g S='>& /dev/null &!', to be used like so: gedit S. This allows me to completely detach it and ignore any output it may give all in one fell swoop. – Reid Jul 23 '11 at 20:33
  • 1
    some implementations of nohup call setsid() to isolate from the process group it was invoked from - but not all (indeed the POSIX spec does NOT) – symcbean Jul 25 '11 at 11:22
  • 1
    The second way works, but the shell still keeps track of that job, which makes the jobs output a bit confusing. – Mikel Nov 06 '12 at 22:22
8

There are two steps involved. One is generally called "backgrounding" and the other "disowning".

  • You launch a background job by appending an ampersand & after the command. This sends the job to the background and allows your shell to continue running. The command you backgrounded is still running as a child process of the shell. You can see it in the list of shell jobs bu running jobs. You could run fg (or fb %N if you have more than one backgrounded job) to bring it to the foreground and send it things like CtrlC.
  • You disown a job by running disown %N where N is the job number. If you only have one backgrounded job this would be disown %1. This kicks the background job "out of the nest" so that it is no longer a child of the shell. You can then close the shell and the disowned program would keep running.

Note: In ZSH you can shortcut the process of disowning by running command &!. The &! backgrounds and disowns in one step.

Caleb
  • 70,105
  • Thanks, @Caleb! Quite solid. But how it compares to nohup? Does it makes both steps? Is it such behavior of nohup just a 'side-effect' and the command itself is intended for different purpose? – Anton Moiseev Jul 23 '11 at 13:06
  • 1
    @Anton, as I understand it, nohup and disown are similar, just that disown is a bash builtin and nohup is a separate command. – glenn jackman Jul 23 '11 at 17:41
  • 3
    @glenn: Not quite. Try it yourself. Nohup is a signal blocker that stops the kill signal from being passed on to the children if the parent gets closed, but it leaves the parent/child/job situation intact. Disown disassociates them so that there is no longer a relation that signals would try to be passed across. – Caleb Jul 23 '11 at 22:00
  • 2
    @Anton: man nohup says run a command immune to hangups. Basically "HUP" is than hang up signal. When you run a job in a shell (even in the background) the process will receive a "hangup" signal if the shell closes. Most programs would choose to close when receiving this, meaning anything you launched from a shell would die with it. Nohup blocks this. It launches your command as a child of itself, and when it receives the nohup signal intentionally does NOT pass it on to the child so that it will keep running even if it's parent dies. Disown is a shell builtin that removes the relationship. – Caleb Jul 23 '11 at 22:08
6

If you have already launched it, you can hit ctrl-z, enter bg and then disown. You will still get output from the application to the terminal though.

Kim
  • 764