30

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

Community
  • 1
  • 1
zanona
  • 12,345
  • 25
  • 86
  • 141

2 Answers2

55

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.

cybersam
  • 63,203
  • 6
  • 53
  • 76
James Polley
  • 7,977
  • 2
  • 29
  • 33
  • Hi James thanks for the info on daemon, it is surely good to know I was wrongly calling it...just changed the question title :) Unfortunately adding an `&` at the end of the line doesn't help, specially when passing arguments like `chromex` if I close terminal it closes chrome at the same time....and also for the second `2>&1 > &1` accuses: `syntax error near unexpected token newline`. the `open` command works only without arguments being passed I believe? – zanona Jun 01 '11 at 12:30
  • 2
    Hi again James, this did the trick `open /Applications/Google\ Chrome.app/ --args --disable-web-security` I believe you must pass the arguments after the `--args` flag...I just edited your answer so I could mark it as correct :) thanks – zanona Jun 01 '11 at 12:39
  • You're right; I had missed that your first alias was calling the second. The & must come at the end of the line - so yes, trying to put arguments after that will break. Let me edit my comment to be slightly trickier, with a function... – James Polley Jun 01 '11 at 12:41
  • Good catch re: "--args". I hadn't spotted that part of your requirements, but even if I had, I didn't know you could do that. – James Polley Jun 01 '11 at 12:50
  • Re: chrome closing when your terminal closes.. I've never had that happen in OSX with terminal.app or iTerm2. I know what you mean - I've had it happen on Linux with various terminals (and I feel like it should happen even on the mac - and indeed, when I go to close Terminal it warns me that closing Terminal will close Chrome)... but it just doesn't happen to me, and I'm not sure why. (While I was typing this I closed the terminal I'd used to start this copy of Chrome, but obviously Chrome is still here, even though Terminal claimed it would go away) – James Polley Jun 01 '11 at 12:52
  • Thanks a lot again for changing the examples to work with the arguments, really useful, not just for this situation I believe, but for many other cases I've been through. cheers – zanona Jun 01 '11 at 14:02
  • `$ alias canary="open /Applications/Google\ Chrome\ Canary.app/ --args --enable-usermedia-screen-capturing" ; $ canary ` - this will launch canary with screenshare –  Sep 10 '14 at 10:19
4

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
david
  • 121
  • 1
  • 2