14

I'm trying to launch a program from the Terminal in such a way that the following occurs:

  1. Closing the Terminal does not close the program
  2. The program is not outputting anything to the Terminal while opening, using, or closing
  3. The Terminal is free to be used for other things
  4. The command is short and sweet
  5. There is no output file to worry about
Evan
  • 321

6 Answers6

15

See man nohup:

NAME
   nohup - run a command immune to hangups, with output to a non-tty

Answer:

nohup program-name &
waltinator
  • 36,399
  • 2
    Almost what I'm looking for. But, it still outputs to the screen when the program is opened and closed: [1]+ Done nohup google-chrome – Evan Apr 16 '15 at 02:00
  • @Evan , yes, you will get these messages, and i don't think there's any way around it, but otherwise it's the best way to separate terminal and the program, and it will be immune to closing the terminal window. Personally, I upvote this answer. Small suggestion , though, you can use nohup program-name 2> /dev/null & to also redirect errors – Sergiy Kolodyazhnyy Apr 16 '15 at 02:49
  • 1
    @Evan that's not nohup, that's bash: http://askubuntu.com/a/590193/158442 – muru Apr 16 '15 at 08:47
  • 1
    The thing I hate about nohup: it doesn't understand aliases in zsh. – Michael Pankov Apr 16 '15 at 13:17
8

I think this would be the most complete way:

program-name </dev/null &>/dev/null &
disown %%

Disowning the backgrounded process means the shell won't track it anymore. It won't tell you when it's done. It won't prevent you from closing your shell. %% means "the most recently backgrounded job".

If you want to hide all the details, put a function like this in your .bashrc

launch_and_forget () { "$@" </dev/null &>/dev/null & disown %%; }

Then

launch_and_forget program args "args args" ...

Give a name that's meaningful for you.

glenn jackman
  • 17,900
  • <>/dev/null 1>&0 2>&0 is slightly shorter than </dev/null &>/dev/null and uses slightly less kernel memory. – kasperd Apr 16 '15 at 10:11
6

You can very simply use setsid:-

NAME
       setsid - run a program in a new session

SYNOPSIS
       setsid program [arg...]

DESCRIPTION
       setsid runs a program in a new session.

Try the following command:-

setsid <command>

Example:- setsid gedit, setsid nautilus ~/Downloads/ etc.

Pandya
  • 35,771
  • 44
  • 128
  • 188
2

Use tmux.

First launch tmux, then run whatever command you want. You can close the tmux window, or drop back by shortcut CTRL-B D. In fact, you can even later reattach by tmux attach.

1

I would recommend either tmux, as Siyuan Ren suggested, or GNU Screen. That way, you get a new terminal for your command. You can detach it and forget about it, but you can get back if you change your mind.

Serpens
  • 111
0

I picked up, and continue to use...

(nohup program-name 2>/dev/null &)