Detach Graphical App from Terminal

November 22, 2009 by Noah
I just stumbled upon a neat trick that can be done in a bash terminal.

Sometimes I start a graphical app from within a terminal, for instance because opening the app via a double-click doesn't do anything (for instance, if a Wine app won't start because of a missing DLL file - I can only see this error message if I try to start the app from within a terminal).

But then I want to close the terminal but still keep the graphical app running. Closing the terminal would kill the graphical app too, unless you can somehow detach it completely from the terminal.

For the example I'll run gedit, GNOME's default text editing program, from a gnome-terminal terminal window.

1) Start the graphical app from the terminal

$ gedit
Now, gedit is running and I don't get a shell prompt in this terminal again unless the app terminates.

2) Temporarily suspend the process with Ctrl-Z

This suspends the currently running process (gedit), giving us a shell prompt again. Since gedit is suspended, its graphical window will become unresponsive -- you can't click any buttons or type. The process is suspended so it can't respond to events like these.

The terminal will tell you which "job number" it has given to the suspended process. This will usually be 1 unless you have other jobs running:

[1]+ Stopped gedit
3) Background the gedit process
$ bg 1
We run the bg command and give it the job number, 1. This puts the process into the background (no longer suspended) and lets it run. So gedit can be running and respond to mouse and keyboard events and you still get a shell prompt in the terminal. However, it isn't yet detached from the terminal. If you close the terminal, gedit will still terminate.

4) Disown all jobs from this terminal

$ disown -h
This command will disown all the current jobs from the terminal. The -h option sets all the jobs to "nohup" mode, so that if the shell that launched the process receives the HUP signal (such as when you close the shell), the signal isn't forwarded to its jobs. So now we can close the gnome-terminal and gedit will continue to run!

This can also be used to detach command-line applications, too - especially if it will be a long-running process (for example, to back up a large folder over the network) and when you don't really care about its output; you just want it to continue running until it's done, but you need to close your terminal.



There are 2 comments on this page. Add yours.

Avatar image
Prakash posted on June 6, 2010 @ 19:44 UTC

No.It fails for Me,when "gxine" was tells "gxine: killed by watchdog bite" followed by "1]+ Aborted gxine" . Any Idea?

Avatar image
isomorphismes posted on February 17, 2014 @ 07:23 UTC

Nice. Thanks. I had always used &, good to know there's an alternative if I forget it.

Add a Comment

Used for your Gravatar and optional thread subscription. Privacy policy.
You may format your message using GitHub Flavored Markdown syntax.