10

I have several instances of Firefox with several different profiles running. Among them profiles with the names "software" and "test".

I am trying to open an URL from a bash script to have it open in profile "test", like this:

firefox -P "test" http://www.example.org/

However that opens it in profile "software" anyway. Any ideas?

Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.8) Gecko/20100308 Iceweasel/3.5.8 (like Firefox/3.5.8)

No, it is not a permissions problem, all my profile directories are perfectly under my permissions:

root@przehyba:~/.mozilla# ls -ld firefox/
drwx------ 13 miernik miernik 4096 Mar 11 09:15 firefox/
root@przehyba:~/.mozilla# ls -ld firefox/*
drwxr-xr-x  9 miernik miernik 4096 Mar 12 11:29 firefox/info
-rw-r--r--  1 miernik miernik  560 Mar 11 09:15 firefox/profiles.ini
drwxr-xr-x 10 miernik miernik 4096 Mar 16 11:51 firefox/software
drwxr-xr-x  9 miernik miernik 4096 Mar 11 09:14 firefox/tech
drwxr-xr-x 11 miernik miernik 4096 Mar 15 22:48 firefox/test
root@przehyba:~/.mozilla# 

9 Answers 9

7

Sorry for the 3 years late answer, I became interested in this topic just now and found this question.

I didn't find a documented solution anywhere, so I checked out the source code and here is a very relevant part: http://dxr.mozilla.org/mozilla-central/source/toolkit/xre/nsAppRunner.cpp#1537

Here, the nullptr at the end of the line unfortunately means that the profile argument is not being passed to the SendCommandLine function, therefore it's not possible to select the correct firefox window to send the openurl message to.

However, we see that the username is being passed and that the username is being intiailized from the LOGNAME environment variable. Based on this, I came up with the following solution:

  • I wrap firefox with this script, called firefox.sh:
FOX_PROFILE=facebook
export LOGNAME=errge.$FOX_PROFILE
/opt/firefox/firefox -profile ~/.mozilla_profiles/$FOX_PROFILES "$@"
  • when ran the first time, it starts a new instance and the ~/.mozilla_profiles/facebook directory has to exist,

  • when ran the second time if the facebook profile is already running, it correctly connects to it,

  • it can send commands correctly to multiple different running profiles (of course you have to change the FOX_PROFILE parameter in the first line in the different scripts).

I tested this with Firefox 26 on Linux and it works.

Alternatively, here is my full-fledged solution that you may want or not want to use:

#!/bin/bash

set -e

FOX=/opt/firefox/firefox

MESSAGE=$@

if [ -z "$FOX_PROFILE" ]; then
  if [ -z "${MESSAGE}" ]; then
    FOX_PROFILE=default
  else
    FOX_PROFILE=$(kdialog --default default --menu "-- ${MESSAGE} --" default default google google facebook facebook errge errge spam spam)
  fi
fi
export FOX_PROFILE

# This hack is needed, because firefox remote command line sending
# ignores the profile parameter.  See nsAppRunner.cpp:1505.
export LOGNAME=errge.$FOX_PROFILE

# Using background execution instead of exec, so the behavior is
# consistent when the profile is already running and when it's just
# starting up.
$FOX -profile ~/.mozilla_profiles/$FOX_PROFILE "$@"

# Huge success.
exit 0

If you run the script without any parameter, it autoselects the default profile, but you can override it by setting FOX_PROFILE by hand in your shell. If an URL is passed in the command line, it always asks for a profile in which to open it, this is because I mainly click through from my chat and email program and in those case I want to always select a profile (google for calendar spam, facebook for birthday spam, etc.). Of course you can change the logic to fit your style of usage, this is just an example, the important knowledge is the LOGNAME trick.

And of course you have to make sure that this wrapper script is the only way to start the browser on your machine. Because if you open a profile without the correct LOGNAME set, than you won't be able to communicate to that profile anymore. I put this script as first in my path via multiple symlinked names, like x-www-browser, firefox, sensible-browser and put it in the BROWSER environment variable too. How to do this exactly depends on your GNU/Linux distribution.

1
  • Thanks so much for this. It seems to me that setting the profile is not necessary and that setting LOGNAME is enough to do the right thing.
    – dset0x
    Dec 26, 2017 at 9:22
1

Based on an answer from Att Righ here 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.)

0

If you already have an instance running, Firefox will reuse that. Try firefox -no-remote.

2
  • But I want it to reuse an instance! Only a specific one, not any random one it likes. If I tried adding "-no-remote" to my command, I get: "Iceweasel is already running, but is not responding. To open a new window, you must first close the existing Iceweasel process, or restart your system."
    – ria
    Mar 12, 2010 at 7:48
  • Ah, that makes it more interesting. I am afraid I do not know how you could do that. IIUC, -no-remote disables "talking" between instances. This may be way off target and total overkill, but you could create an extension that polls a central file or server URL queue and opens it when it is meant for the current profile. You then install that extension in all of your instances.
    – janmoesen
    Mar 12, 2010 at 7:54
0

@miemik

"But I want it to reuse an instance! Only a specific one, not any random one it likes. If I tried adding "-no-remote" to my command, I get: "Iceweasel is already running, but is not responding. To open a new window, you must first close the existing Iceweasel process, or restart your system."

You have a permission problem with your profile dir...check the permission. Did you copy or move it from anyother place(partition..disk..)?

Solution:

Become root and run top to see if it's running as root. Then ls -ld on the profile dir(whichh should be under ur home dir with a period(.) in front of them..means you have to use "ls -al" to see it.

Verify other permission of that dir and change accordingly.

Cheers!

1
  • I checked the permissions and pasted the result in an edit to my question. It is not that problem.
    – ria
    Mar 16, 2010 at 10:56
0

It won't work. The only way to have 2 instances of a Mozilla app running is by disabling remote connections to the second one (usually with MOZ_NO_REMOTE=1). That means you disable the ability for it to communicate with other instances (like one creating opening a new page).

So your choices are either to open a new app with no remote enabled, or to reuse the instance that allows remote access.

1
  • It indeed works, see errge's answer. What do you mean by it won't work?
    – pts
    Jan 17, 2014 at 12:47
0

You should be able to add the -no-remote to only the test profile call (after the software profile is already open) like this:

firefox -P "test" -no-remote http://www.example.org/

That should allow all other links to still open in your other profile remotely.

If this solution does not work you could always run Firefox as a separate user for testing, using sudo -u. Firefox really should be able to handle this properly with profiles though.

1
  • This answer can't be good because with the -no-remote flag a new Firefox instance is started, and the question requires reusing the existing Firefox instances if they are already running.
    – pts
    Jan 17, 2014 at 12:46
0

If you have Mozilla Firefox already open with the Default profile or anyother profile, try:

Path-To/firefox -remote openurl("http://www.example.org")

Tested and worked for me with Mozilla Firefox from 2.x to 12.x

1
  • This answer can't be good because the command-line doesn't contain the required profile to use.
    – pts
    Jan 17, 2014 at 12:45
0

The first time you start a Firefox instance with a given profile, use the command:

firefox -P foo -new-instance

You can append a URL at the end of the command above to open it. Later, when you want to open a URL in that instance, you can use:

firefox -P foo -remote 'openurl(http://www.example.org)'

If you are writing a script and want to find out whether there's already an instance running that profile to see which of the commands above should be called, you can use:

firefox -P foo -remote 'ping()'

Tested with Firefox 34 on Linux.

Note that there's currently a bug in Firefox which makes it error out with the message

Error: Failed to send command: 500 command not parseable

if the -remote openurl() command is made without a controlling tty (e.g., from a script started by a daemon).

I hit this bug when launching a script from a .desktop file in KDE (e.g., from a launcher in the panel), so I had to tick the "run in terminal" option, which is a bit annoying.

-1

For the record, one solution is: in KDE we set the default browser to run with the following command:

firefox -P "default"

where "default" is our profile we want to use each time.

Now, we start our Firefox instance with no arguments (the default KDE Firefox launcher). This opens Firefox normally, now we want to open another profile, to do so, we make another launcher in KDE menu editor:

firefox -ProfileManager -no-remote %u

This opens the profile manager, we choose which profile to use.

Tested on several profiles concurrently running, when we click on an url from any app (like a chat client), a new tab is then opened in the profile we've chosen.

Hope someone finds this helpful :).

Not tested on Gnome, but I hope it could be replicated.

1
  • 1
    This does not answer the question, the OP wants to launch firefox with different profiles from a script automatically.
    – terdon
    Oct 11, 2012 at 23:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .