Linux – how to remotely open an URL in Firefox in a specific profile

firefoxlinuxmultiple instancesremoteuser-profiles

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# 

Best Answer

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.

Related Question