run Mac Chrome with command line arguments as a background process

44,972

Solution 1

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.

Solution 2

I defined alias on my .zshr (same for .bash_profile) like this:

open_by_browser(){ open -a $1 $2}
alias firefox='open_by_browser firefox'
alias chrome='open_by_browser "Google Chrome"'

then I can open html file by Firefox or Chrome

for example, by Firefox

firefox xxx/index.html

by Chrome

chrome xxx/index.html
Share:
44,972
zanona
Author by

zanona

My primary focus over the past 15 years has been full-stack web development and implementation, experimenting with technologies while thinking openly about how tech and users can interact. Currently, acting as a web consultant for goal-oriented businesses and concentrating on designing interactive systems, UX research/ implementation and engineering/development of elaborate online platforms.

Updated on August 30, 2020

Comments

  • zanona
    zanona almost 4 years

    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