How to start GUI linux programs from the command line, but separate from the command line?

90

41

I've searched for this before, but have never been able to find an answer.

In Windows, if I have a console window open, type winmine, and press enter, Minesweeper will appear, completely separate from the cmd program. The Minesweeper instance is not tied to the command prompt in any way that I know of, with the exception of Minesweeper's parent being set to that instance of the command prompt. It's different in Linux, however.

In Linux, if I have a console window open, type emacs and press enter, Emacs will open, but it seems tied to the command line. Specifically, it appears that I can't use the command line anymore until that instance of Emacs is closed. Is there a way to replicate the Windows behavior in Linux?

Thanks!

Jason

Posted 2010-08-18T03:22:58.697

Reputation: 1 010

please give the answer to geekosaur. – Joshua Robison – 2015-09-04T13:30:31.750

in windows7 it's minesweeper.exe here C:\Program Files\Microsoft Games\Minesweeper so minesweeper.exe won't generally work.. as that directory is not in the path.. And anyhow, you should state windows version – barlop – 2015-11-10T00:15:07.193

Answers

10

In bash, detach is a built-in, used as follows:

emacs &
detach %+ # or % + the number in brackets printed after the above

In zsh, you can background and detach in a single operation:

emacs &|

geekosaur

Posted 2010-08-18T03:22:58.697

Reputation: 10 195

109

Append & to the commandline:

emacs &

This will put emacs in the background and enable you to continue using your terminal.

Note that this will still leave emacs as a sub-process of your terminal, and when you exit the terminal it will also exit emacs. To avoid this, type:

(emacs &)

The parentheses tell the terminal to detach the emacs process from the terminal.

Still, stdout and stderr messages from the program will show up on the terminal. To prevent this, use:

(emacs &> /dev/null &)

Nathan Fellman

Posted 2010-08-18T03:22:58.697

Reputation: 8 152

When pressing Ctrl+C in the terminal window this command was issued, the process is killed :< – Richard de Wit – 2016-02-26T06:46:53.297

What if I want to make sure their standard error or standard out goes to the X error logging mechanism? For example see: http://unix.stackexchange.com/questions/2574/where-do-x-error-messages-go Normally if I start a process in the GUI, its errors gets sent to ~/.xsession-errors.

– CMCDragonkai – 2016-04-14T11:29:17.157

Finally someone who understood the question. I was looking for Windows bash equivalent of start <program> and this seems to be it. – Igor Čordaš – 2017-01-18T12:23:53.610

9I especially like the (emacs &) tip. – None – 2010-08-18T05:05:21.693

1if you have already started the task (emancs), then use CTRL-Z to put to sleep and then use command "bg" to send to background. – Jayan – 2010-08-18T05:16:08.597

@StackTrace: if you do that, and you close your terminal, it will also kill emacs. – Nathan Fellman – 2011-07-02T21:34:36.323

1OK.. I used to use "nohup" detach this. It cannot be done once the process is started ... – Jayan – 2011-07-04T17:44:49.020

21

Use nohup. Like this: nohup amarok &

Hope this helps.

NawaMan

Posted 2010-08-18T03:22:58.697

Reputation: 311

19

I posted an answer to an older thread of similar topic with answers from various sources. Following is a copy of that answer adapted for this thread.


Following works:

$ (gui_app &> /dev/null &)

This is Nathan Fellman's answer plus redirection.

"&> /dev/null" redirects both stdout and stderr to the null device. The last ampersand makes the process run in the background. The parentheses around the command will cause your "gui_app" to run in a subshell.

Doing this will detach the "gui_app" process from the console you execute this command from. So even if you close the window parent terminal emulator is running in, "gui_app" won't close. I ran this then looked at the process tree with "pstree" command and found an application started this way will become child process to "init".

For example,

$ gui_app &> /dev/null &

will run the application in the background, but it will become a child process of the console process and will terminate when you close the terminal. (Though exiting the terminal through bash by using the exit command or Ctrl-D will let bash clean up by handing off the background process to init.)

"nohup" works as NawaMan has suggested, but that redirects output & error to a file by default. As JeffG has answered, "disown" command (if available in shell) can detach process from terminal after you've started a background process:

$ gui_app &
$ disown

(BTW all of this applies to bash. I'm sure other shells have other methods/syntax for doing this.)

Some reference: Disowning Processes (UNIX Power Tools)

If it's a simple call to a GUI application - without complicated options and such - it seems using a launcher like "gmrun" or dmenu (warning: loud audio) is also a good option. Bind it to a key combination. I don't use a launcher yet but have tried those two.

NOTE: CarlF in the comments of the other thread reports GUI apps started via "gui_app &" method does not close when he exits from the parent terminal. I think that we were closing the terminal in different ways. I was closing the window the terminal emulator was running in. I think he may have been exiting the terminal emulator through the shell (exit command or Ctrl-D). I tested this and saw that exiting through bash does not stop GUI started as terminal's background process as CarlF says. It seems bash hands off background processes to init when it is given the chance to clean up. In fact, this must be the mechanism by which the background process started in a subshell gets handed off to init.

EMPraptor

Posted 2010-08-18T03:22:58.697

Reputation: 376

11

util-linux includes a tool called setsid which basically does what detach does:

$ setsid someprogram

This will run someprogram in a new session.

tonybaldwin

Posted 2010-08-18T03:22:58.697

Reputation: 111

9

Try typing xemacs & to open XEmacs in the background.

Please don't refer to this as "emulating Windows behavior". Ouch.

You could also:

  • Open a new terminal
  • Use Alt+F2 or your window manager to launch the program, instead of using the terminal
  • Use GNU Screen

Borealid

Posted 2010-08-18T03:22:58.697

Reputation: 1 108

7

I put the following code in a an executable script file named "sbg" for "silent background". It does all the necessary things to completely disconnect the program being started from the shell: redirects stdin, stdout and stderr from/to /dev/null, ignores hangups, runs in background, and detaches.

#!/bin/bash
nohup "$@" &>/dev/null & disown %%

Then you can just do sbg emacs. You can also pass any arguments you want to: sbg emacs --daemon FILE1 FILE2.

Ryan C. Thompson

Posted 2010-08-18T03:22:58.697

Reputation: 10 085

the scripting solution worked best for me. thanks!! although I used (gui_app &> /dev/null &) instead (@EMPraptor's answer) – Jimi Oke – 2016-10-12T14:58:17.017

3

command &

Put an ampersand after the command.

Jesse Dhillon

Posted 2010-08-18T03:22:58.697

Reputation: 130

3

if you are using bash, you can disown the process:

% firefox &
% disown 

JeffG

Posted 2010-08-18T03:22:58.697

Reputation: 351

2

You can use this single line:

$ emacs & disown

Idham Perdameian

Posted 2010-08-18T03:22:58.697

Reputation: 121

1

As the other answers say, you can use the following:

$ emacs &

If you forget the & though, just press Ctrl-z then use bg:

[1]+  Stopped                 emacs
$ bg
[1]+ emacs &
$

FYI: Ctrl-z stops the emacs process and bg sends into the background.

MatthewD

Posted 2010-08-18T03:22:58.697

Reputation: 313

0

Put this to your ~/.bashrc:

debug_trap_skip()
{
    local cmd_type=$(type -t "$1")

    # Empty cmd_type is for inexistent command or variable definition, exclude them
    # and shell builtins except echo, set and env:
    [[ -z "$cmd_type" || "$cmd_type" = builtin && "$1" != echo && "$1" != set && "$1" != env ]] &&
        return 0

    return 1
}

debug_trap_x11()
{
    if ldd `which $1`|grep -q libX11
    then
        setsid "$@"
        return 0
    fi
    return 1
}

debug_trap()
{
    [[ -n "$COMP_LINE" ]] && return  # do nothing if completing
    [[ "$BASH_COMMAND" = "$PROMPT_COMMAND" ]] && return
    debug_trap_skip $BASH_COMMAND && return 0
    debug_trap_x11 $BASH_COMMAND && return 1

    return 0
}

shopt -s extdebug
trap debug_trap DEBUG

Now you don't have to remember which programs are X11 and do special manipulations for them because they will detach automatically. Note that setsid is better than nohup because the last one blocks HUP signal which is not what you need.

P.S. Now you are winmine-ready in Linux.

midenok

Posted 2010-08-18T03:22:58.697

Reputation: 190

0

just using & as in $ x & leaves the process attached to the terminal i picked up a small program called detach http://inglorion.net/software/detach/ which i have aliased as d it is a bit like nohup but doesn't leave behind a log file i use it to detach mupdf: d mupdf x.pdf

Dan D.

Posted 2010-08-18T03:22:58.697

Reputation: 5 138