88

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!

migrated from stackoverflow.com Aug 18 '10 at 6:10

This question came from our site for professional and enthusiast programmers.

  • 1
    dup of:superuser.com/questions/178587/… – behrooz Apr 25 '11 at 18:07
  • please give the answer to geekosaur. – Joshua Robison Sep 4 '15 at 13:30
  • 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 Nov 10 '15 at 0:15

13 Answers 13

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 &|
108

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 &)
  • 9
    I especially like the (emacs &) tip. – Jason Aug 18 '10 at 5:05
  • 1
    if 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 Aug 18 '10 at 5:16
  • @StackTrace: if you do that, and you close your terminal, it will also kill emacs. – Nathan Fellman Jul 2 '11 at 21:34
  • 1
    OK.. I used to use "nohup" detach this. It cannot be done once the process is started ... – Jayan Jul 4 '11 at 17:44
  • When pressing Ctrl+C in the terminal window this command was issued, the process is killed :< – Richard de Wit Feb 26 '16 at 6:46
21

Use nohup. Like this: nohup amarok &

Hope this helps.

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.

11

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

$ setsid someprogram

This will run someprogram in a new session.

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
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.

  • the scripting solution worked best for me. thanks!! although I used (gui_app &> /dev/null &) instead (@EMPraptor's answer) – Jimi Oke Oct 12 '16 at 14:58
3
command &

Put an ampersand after the command.

3

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

% firefox &
% disown 
2

You can use this single line:

$ emacs & disown
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.

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

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.

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.