Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run Mac Chrome with command line arguments as a background process

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

like image 750
zanona Avatar asked Jun 01 '11 11:06

zanona


People also ask

How do you run a command in the background on a Mac?

If you know you want to run a command in the background, type an ampersand (&) after the command as shown in the following example. The number that follows is the process id. The command bigjob will now run in the background, and you can continue to type other commands.

How do I run Chrome from the command line?

Open Chrome Using Command Prompt You can also do the same thing from the “Run” window. Open Run by typing “Run” in the Windows 10 search bar and selecting the “Run” application. Here, type Chrome and then select the “OK” button. The web browser will now open.


1 Answers

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.

like image 115
James Polley Avatar answered Sep 21 '22 15:09

James Polley