7

I have two instances of firefox running under separate profiles:

$ firefox -P default &
...
$ firefox -no-remote -P second &

Now I can open a new tab from the command line with:

$ firefox -new-tab http://unix.stackexchange.com

But how do I open a new tab in the second profile?

This:

$ firefox -P second -new-tab http://unix.stackexchange.com

opens a tab in the default profile, while:

$ firefox -no-remote -P second -new-tab http://unix.stackexchange.com

complains that there is already an instance running under that profile.

Att Righ
  • 1,196
chris
  • 1,142

3 Answers3

3

It works now with firefox 29.0 on Linux:

To open a second firefox instance with a different profile:

firefox -P second -new-instance

To open a new tab in the second instance of firefox, which os already running:

firefox -P second -remote "openurl(http://example.com,new-tab)"


See Bug 716110 - split -new-instance flag out of existing -no-remote flag for additional hints (eg: post of Hayo).

As explained in the comments on this bug report, what is missing is a command that can be used for opening the first window and the second tab in the same way:

That could be done with a script along the lines of this (firefox-profile-instance):

#!/bin/bash

PROFILE="$1"
URL="$2"

if firefox -P "$PROFILE" -remote "ping()" >/dev/null 2>&1 ; then
    firefox -P "$PROFILE" -remote "openurl($URL,new-tab)"
else
    firefox -P "$PROFILE" -new-instance "$URL" &
fi

Now, while a firefox with the default profile is already running,
the first run of this starts a new browser with profile "second":

firefox-profile-instance second "http://example.com"

and running the same again opens a second tab in the same browser:

firefox-profile-instance second "http://example.com"

Volker Siegel
  • 17,283
  • 1
    The -remote option has been removed from firefox. However, the existing commands -new-window, -new-tab and the generic open command have been extended to support the -P argument (true in firefox 52).. – Att Righ Nov 26 '17 at 20:04
  • 1
    There appears to be no equivalent to the -remote ping() option. This can be replaced with a call to pgrep (see answer below). – Att Righ Nov 26 '17 at 20:18
1

This answer is very much an extension of Volker Siegel's answer above and I'm happy for the two to be merged. I am only writing this to format a new script given that firefox no longer supports -remote.

New versions of Firefox (tested with version 52) support a -new-instance option for spawning a new instance that supports remote calls. Subsequent calls to firefox, if given a -P argument, will carry out actions in a running firefox profile with the given profile name.

If you wish to combine spawning and link opening into one script then this can be achieved using pgrep as shown below:

#!/bin/bash
profile=profile-name
if pgrep --full "^firefox-esr\b.*$profile" > /dev/null; then
    firejail --profile=$HOME/.firejail/firefox.jail.profile firefox -P "$profile" "$@" > /dev/null
else
    firejail --profile=$HOME/.firejail/firefox.jail.profile firefox -new-instance -P "$profile" "$@"
    disown $!
fi

(Warning this script has not been tested, but is adapted from one that I use)

Att Righ
  • 1,196
1

Based on the answer from @Att Righ I developed the following solution which automatically selects the right profile based on the URL.

This wrapper script is tested on Ubuntu Linux 20.04.6 with Mozilla Firefox 104.0.

#!/bin/bash

if [[ "$@" =~ ."google."|"facebook.com"|"instagram.com". ]] then profile=for_evil_sites else profile=default fi

if pgrep --full "firefox\b.*$profile" > /dev/null; then /usr/bin/firefox -P "$profile" "$@" > /dev/null else /usr/bin/firefox --new-instance -P "$profile" "$@" > /dev/null disown $! fi

Save the script with the name firefox e.g. in $HOME/bin/ and make sure it will be loaded instead of your standard firefox. (The directory has to be before the original one in variable $PATH.)

AdminBee
  • 22,803