Skip to main content
fixed description of the command’s parts; replaced dead link with the archived version
Source Link

Building off of ngoozeff's answer, if you want to make a command run completely in the background (i.e., if you want to hide its output and prevent it from being killed when you close its Terminal window), you can do this instead:

cmd="google-chrome"; "${cmd}" &>/dev/null &disown;& disown; 

& (the first one) detaches the command from stdin.
>/dev/null detaches the shell session from stdout and stderr.
&disown removes the command from the shell's job list.

  • &>/dev/null sets the command’s stdout and stderr to /dev/null instead of inheriting them from the parent process.
  • & makes the shell run the command in the background.
  • disown removes the “current” job, last one stopped or put in the background, from under the shell’s job control.

YouIn some shells you can also use &! instead of &disown& disown; they'rethey both have the same commandeffect. Bash doesn’t support &!, though.

Also, when putting a command inside of a variable, it's more proper to use eval "${cmd}" rather than "${cmd}":

cmd="google-chrome"; eval "${cmd}" &>/dev/null &disown;& disown; 

If you run this command directly in Terminal, it will show the PID of the process which the command starts. But inside of a shell script, no output will be shown.

Here's a function for it:

#!/bin/bash # Run a command in the background. _evalBg() { eval "$@" &>/dev/null &disown;& disown; } cmd="google-chrome"; _evalBg "${cmd}"; 

http://felixmilea.com/2014/12/running-bash-commands-background-properly/ Also, see: Running bash commands in the background properly

Building off of ngoozeff's answer, if you want to make a command run completely in the background (i.e., if you want to hide its output and prevent it from being killed when you close its Terminal window), you can do this instead:

cmd="google-chrome"; "${cmd}" &>/dev/null &disown; 

& (the first one) detaches the command from stdin.
>/dev/null detaches the shell session from stdout and stderr.
&disown removes the command from the shell's job list.

You can also use &! instead of &disown; they're both the same command.

Also, when putting a command inside of a variable, it's more proper to use eval "${cmd}" rather than "${cmd}":

cmd="google-chrome"; eval "${cmd}" &>/dev/null &disown; 

If you run this command directly in Terminal, it will show the PID of the process which the command starts. But inside of a shell script, no output will be shown.

Here's a function for it:

#!/bin/bash # Run a command in the background. _evalBg() { eval "$@" &>/dev/null &disown; } cmd="google-chrome"; _evalBg "${cmd}"; 

http://felixmilea.com/2014/12/running-bash-commands-background-properly/

Building off of ngoozeff's answer, if you want to make a command run completely in the background (i.e., if you want to hide its output and prevent it from being killed when you close its Terminal window), you can do this instead:

cmd="google-chrome"; "${cmd}" &>/dev/null & disown; 
  • &>/dev/null sets the command’s stdout and stderr to /dev/null instead of inheriting them from the parent process.
  • & makes the shell run the command in the background.
  • disown removes the “current” job, last one stopped or put in the background, from under the shell’s job control.

In some shells you can also use &! instead of & disown; they both have the same effect. Bash doesn’t support &!, though.

Also, when putting a command inside of a variable, it's more proper to use eval "${cmd}" rather than "${cmd}":

cmd="google-chrome"; eval "${cmd}" &>/dev/null & disown; 

If you run this command directly in Terminal, it will show the PID of the process which the command starts. But inside of a shell script, no output will be shown.

Here's a function for it:

#!/bin/bash # Run a command in the background. _evalBg() { eval "$@" &>/dev/null & disown; } cmd="google-chrome"; _evalBg "${cmd}"; 

Also, see: Running bash commands in the background properly

Consistent syntax
Source Link
GreenRaccoon23
  • 3.9k
  • 8
  • 37
  • 47

Building off of ngoozeff's answer, if you want to make a command run completely in the background (i.e., if you want to hide its output and prevent it from being killed when you close its Terminal window), you can do this instead:

cmd="google-chrome"chrome"; "${cmd}" &>/dev/null &disown&disown; 

& (the first one) detaches the command from stdin.
>/dev/null detaches the shell session from stdout and stderr.
&disown removes the command from the shell's job list.

You can also use &! instead of &disown; they're both the same command.

Also, when putting a command inside of a variable, it's more proper to use eval "${cmd}" rather than "${cmd}":

cmd="google-chrome"chrome"; eval "${cmd}" &>/dev/null &disown&disown; 

If you run this command directly in Terminal, it will show the PID of the process which the command starts. But inside of a shell script, no output will be shown.

Here's a function for it:

#!/bin/bash # Run a command in the background. _evalBg() { eval "$@" &>/dev/null &disown; } cmd="google-chrome"; _evalBg "${cmd}"; 

http://felixmilea.com/2014/12/running-bash-commands-background-properly/

Building off of ngoozeff's answer, if you want to make a command run completely in the background (i.e., if you want to hide its output and prevent it from being killed when you close its Terminal window), you can do this instead:

cmd="google-chrome" "${cmd}" &>/dev/null &disown 

& (the first one) detaches the command from stdin.
>/dev/null detaches the shell session from stdout and stderr.
&disown removes the command from the shell's job list.

You can also use &! instead of &disown; they're both the same command.

Also, when putting a command inside of a variable, it's more proper to use eval "${cmd}" rather than "${cmd}":

cmd="google-chrome" eval "${cmd}" &>/dev/null &disown 

If you run this command directly in Terminal, it will show the PID of the process which the command starts. But inside of a shell script, no output will be shown.

Here's a function for it:

#!/bin/bash # Run a command in the background. _evalBg() { eval "$@" &>/dev/null &disown; } cmd="google-chrome"; _evalBg "${cmd}"; 

http://felixmilea.com/2014/12/running-bash-commands-background-properly/

Building off of ngoozeff's answer, if you want to make a command run completely in the background (i.e., if you want to hide its output and prevent it from being killed when you close its Terminal window), you can do this instead:

cmd="google-chrome"; "${cmd}" &>/dev/null &disown; 

& (the first one) detaches the command from stdin.
>/dev/null detaches the shell session from stdout and stderr.
&disown removes the command from the shell's job list.

You can also use &! instead of &disown; they're both the same command.

Also, when putting a command inside of a variable, it's more proper to use eval "${cmd}" rather than "${cmd}":

cmd="google-chrome"; eval "${cmd}" &>/dev/null &disown; 

If you run this command directly in Terminal, it will show the PID of the process which the command starts. But inside of a shell script, no output will be shown.

Here's a function for it:

#!/bin/bash # Run a command in the background. _evalBg() { eval "$@" &>/dev/null &disown; } cmd="google-chrome"; _evalBg "${cmd}"; 

http://felixmilea.com/2014/12/running-bash-commands-background-properly/

added the `&!` alternative
Source Link
GreenRaccoon23
  • 3.9k
  • 8
  • 37
  • 47

Building off of ngoozeff's answer, if you want to make a command run completely in the background (i.e., if you want to hide its output and prevent it from being killed when you close its Terminal window), you can do this instead:

cmd="google-chrome" "${cmd}" &>/dev/null &disown 

& (the first one) detaches the command from stdin.
>/dev/null detaches the shell session from stdout and stderr.
&disown removes the command from the shell's job list.

You can also use &! instead of &disown; they're both the same command.

Also, when putting a command inside of a variable, it's more proper to use eval "${cmd}" rather than "${cmd}":

cmd="google-chrome" eval "${cmd}" &>/dev/null &disown 

If you run this command directly in Terminal, it will show the PID of the process which the command starts. But inside of a shell script, no output will be shown.

Here's a function for it:

#!/bin/bash # Run a command in the background. _evalBg() {   local cmd="$@";  eval "${cmd}""$@" &>/dev/null &disown; } cmd="google-chrome"; _evalBg "${cmd}"; 

http://felixmilea.com/2014/12/running-bash-commands-background-properly/

Building off of ngoozeff's answer, if you want to make a command run completely in the background (i.e., if you want to hide its output and prevent it from being killed when you close its Terminal window), you can do this instead:

cmd="google-chrome" "${cmd}" &>/dev/null &disown 

& (the first one) detaches the command from stdin.
>/dev/null detaches the shell session from stdout and stderr.
&disown removes the command from the shell's job list.

Also, when putting a command inside of a variable, it's more proper to use eval "${cmd}" rather than "${cmd}":

cmd="google-chrome" eval "${cmd}" &>/dev/null &disown 

If you run this command directly in Terminal, it will show the PID of the process which the command starts. But inside of a shell script, no output will be shown.

Here's a function for it:

#!/bin/bash # Run a command in the background. _evalBg() {   local cmd="$@";  eval "${cmd}" &>/dev/null &disown; } cmd="google-chrome"; _evalBg "${cmd}"; 

http://felixmilea.com/2014/12/running-bash-commands-background-properly/

Building off of ngoozeff's answer, if you want to make a command run completely in the background (i.e., if you want to hide its output and prevent it from being killed when you close its Terminal window), you can do this instead:

cmd="google-chrome" "${cmd}" &>/dev/null &disown 

& (the first one) detaches the command from stdin.
>/dev/null detaches the shell session from stdout and stderr.
&disown removes the command from the shell's job list.

You can also use &! instead of &disown; they're both the same command.

Also, when putting a command inside of a variable, it's more proper to use eval "${cmd}" rather than "${cmd}":

cmd="google-chrome" eval "${cmd}" &>/dev/null &disown 

If you run this command directly in Terminal, it will show the PID of the process which the command starts. But inside of a shell script, no output will be shown.

Here's a function for it:

#!/bin/bash # Run a command in the background. _evalBg() { eval "$@" &>/dev/null &disown; } cmd="google-chrome"; _evalBg "${cmd}"; 

http://felixmilea.com/2014/12/running-bash-commands-background-properly/

prettified
Source Link
GreenRaccoon23
  • 3.9k
  • 8
  • 37
  • 47
Loading
made some syntax improvements
Source Link
GreenRaccoon23
  • 3.9k
  • 8
  • 37
  • 47
Loading
Source Link
GreenRaccoon23
  • 3.9k
  • 8
  • 37
  • 47
Loading