run Mac Chrome with command line arguments as a background process

zanona picture zanona · Jun 1, 2011 · Viewed 44.3k times · Source

I have 2 aliases on my .bash_profile file containing:

alias chrome="/Applications/Google\\ \\Chrome.app/Contents/MacOS/Google\\ \\Chrome"
alias chromex="chrome --disable-web-security"

but when running, it opens up Chrome but keeps holding the terminal window...once I close the terminal window it also closes chrome.

Is there any way to make it run in the background?

I remembered I use this for thin webserver with thin start -d or thin start --daemonize?

Thanks


update

besides James answer I also found the nohup command line which made possible for me to quit terminal without problem that was a mix by appending the & to the nohup command:

$ nohup chromex &

the default output is written to the nohup.out file

To stop the job I can run ps ax, find the PID though the right command and then kill -9 PID

Answer

James Polley picture James Polley · Jun 1, 2011

Put an ampersand on the end of the commandline.

alias chrome="/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome &"

If you also don't want to see any of the debugging chrome outputs, redirect stdout and stderr to /dev/null

alias chrome="/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome 2>&1 > &"

On Mac, you can make this even simpler:

alias chrome="open /Applications/Google\ Chrome.app/ --args --disable-web-security"

Your second requirement makes this slightly trickier though. The & needs to be at the end of the commandline; but your second alias adds commands to the end of the first command - ie, after the ampersand - and so this doesn't work.

To get around this, we can redefine 'chrome' as a function.

chrome () {
  /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome $* 2>&1 &
}

The $* means that any commandline parameters passed to the function will be inserted here, before the ampersand. This means you can still define your second alias as

alias chromex="chrome --disable-web-security"

This will be expanded out to

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --disable-web-security 2>&1 &

BTW, this is just referred to as running "in the background". "As a daemon" would refer to a server process that runs whenever the machine is turned on, and is not tied to any user's session.