96

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!

3
  • 1
    dup of:superuser.com/questions/178587/…
    – behrooz
    Apr 25, 2011 at 18:07
  • please give the answer to geekosaur. Sep 4, 2015 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, 2015 at 0:15

16 Answers 16

13

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 &|
0
118

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

Use nohup. Like this: nohup amarok &

Hope this helps.

20

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.

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

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

Put an ampersand after the command.

3

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

% firefox &
% disown 
3

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.

3

You can use this single line:

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

0

If none of the above works because DISPLAY is not set, then use

(xvfb-run &)

For explanation see https://superuser.com/a/1681455/269343.

2
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
    – ZygD
    Oct 13, 2021 at 7:41
  • On the other, we ought to avoid duplication of discussions on SE.
    – Joachim W
    Oct 14, 2021 at 10:11
0

When you run the X application, it starts up. Using the command line, what i always do is press:

Ctrl+Z this suspends the application in the command line and returns the shell to a prompt. Now it can't stay like this because the X application is now frozen.

To resume it and keep it detached from the command line:

type in the prompt:

bg

And this will restart the X window app in the background but you'll have control of over your command line and you X app will now be able to resume operation!

These two steps are the equivalent of running <./application> & mentioned in other answers, except this way you don't have to quit or restart the X app.

-3

You can run any application in the background so it won't be tied to the terminal. Just put an ampersand after the command.

emacs &

That will run emacs but not tie it to the terminal. Hope this helps!

1
  • This duplicates other answers and doesn't add anything. If you think you have an answer, please read any existing answers and do not post yours if it is not new or different in some significant way. Aug 28, 2020 at 20:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .