Tips for Spawning GUI’s from the Command Line

I love the command line. However, most people can’t just live on the command line, GUI’s can be pretty  useful :P. I find myself spawning GUI programs from the command line a lot, and here are two tips which make doing that a pleasant experience:

‘Pausing’ programs and Running them in the Background.

When you run a command, notice how the output for that command ‘takes over’ the terminal? Sometimes you want to run a GUI program in a terminal, and have it run in the background, without tying up the prompt. Here’s how to do that…

Run a program like this:

 kevin@maui:~$ vlc movie.mpg

notice how you can’t type anything now, you’ve lost your prompt. Just hit ctrl-z, and the program will pause. You’ll get control of your commandline again! You program is paused though, and likely of no use paused. If you want your program to continue running in the background just type in “bg” for background. If you want your program to hijack you prompt again, run “fg” for “foreground”

kevin@maui: $ bg     #Run paused program in the background
kevin@maui: $ fg     #Run paused program in the foreground again

Detaching GUI processes from the terminal process

One thing that bothered me for a long time is when I run a GUI program from a terminal, if you close the terminal after, the GUI closes right along behind it. Why? The GUI program is spawned as a child process of the terminal, so closing the parent process will send kill signals to all the child processes. If you want to run a GUI and have it run independently of its parent’s process, run your commands like this:

 kevin@maui:~$ vlc movie.mpg & disown

Now your player stays open after closing the terminal you spawned it from! You don’t have to have one terminal instance open for every GUI you spawn now! It took me a while to learn that trick, use it to great effect 😀

There are many other things you can do to control processes from the command line, but I’ve found those two tricks the most handy when running GUI’s from the command line.

This entry was posted in Open Source, Ubuntu. Bookmark the permalink.

10 Responses to Tips for Spawning GUI’s from the Command Line

  1. Rick Boyer says:

    Great Blog post. I am going to bookmark and read more often. I love the Blog template

  2. Mackenzie says:

    I always use nohup for that last bit. “nohup evince thing.pdf &” and then I can close the terminal.

  3. anonymous user says:

    i cant believe i didnt know this till now
    thanks.
    any more tips much appreciated

  4. Christoph says:

    One thing that bothered me for a long time is when I run a GUI program from a terminal, if you close the terminal after, the GUI closes right along behind it. Why? The

    Why so complicated? Start your program and use “&” to detach it from the shell

    # vlc movie.mpg &

    and exit your terminal application properly

    # exit

    VLC will keep on playing.

  5. Kevin says:

    With my default install of ubuntu and using gnome-terminal, I have to used disown. A terminating ampersand has doesn’t tie up the prompt, but closing gnome-terminal closes the GUI. I haven’t really tried it with any other terminal application, although, I can imagine different behaviors among different terminal applications.

  6. Matt Trudel says:

    Indeed, nohup as identified by Mackenzie is a good idea… It also brings you a few additional benefits to disown: you can still close the terminal, and your program’s output is, by default, sent to nohup.out! Great for debugging apps.

  7. Bryan Forbes says:

    I’ve found an easier way to do the last tip:
    (vlc movie.mpg &)

    Effective, yet only two more keystrokes :).

  8. Mackenzie says:

    Christoph:
    That just backgrounds it so you can run more commands. It doesn’t detach it from the shell. Maybe a few apps will detach when backgrounded, but certainly not all of them.

  9. Scott Inderlied says:

    How can i make this work with an app that requires sudo/root? I get a error e.g. [1] 10190.

  10. Christopher says:

    Thanks! I knew about the first trick, but not the second. I hereby grant you the “best blogger of the day” award.

Leave a Reply

Your email address will not be published. Required fields are marked *