Can't run Firefox in headless mode as www-data user

Asked by Stan

Hi everyone,

I'm trying to run firefox in headless mode (for use with selenium) as the www-data user but it doesn't appear that firefox gets fully launched. There are no obvious errors but the correct processes don't appear to spawn either.

System: Ubuntu Server 20.04.2
Firefox 86

Here is what a successful run as a regular user looks like:

===

> firefox --headless
*** You are running in headless mode.
[GFX1-]: glxtest: Unable to open a connection to the X server
[GFX1-]: glxtest: libEGL initialize failed

(/usr/lib/firefox/firefox:757337): GLib-GObject-CRITICAL **: 08:37:03.446: g_object_set: assertion 'G_IS_OBJECT (object)' failed

(/usr/lib/firefox/firefox:757404): GLib-GObject-CRITICAL **: 08:37:04.235: g_object_set: assertion 'G_IS_OBJECT (object)' failed

(/usr/lib/firefox/firefox:757434): GLib-GObject-CRITICAL **: 08:37:04.815: g_object_set: assertion 'G_IS_OBJECT (object)' failed

(/usr/lib/firefox/firefox:757497): GLib-GObject-CRITICAL **: 08:37:05.948: g_object_set: assertion 'G_IS_OBJECT (object)' failed
^CExiting due to channel error.
Exiting due to channel error.
Exiting due to channel error.
Exiting due to channel error.

===

Here is the output of ps during the run:

===
glenn 757284 173736 45 08:37 pts/2 00:00:06 /usr/lib/firefox/firefox --headless
glenn 757337 757284 15 08:37 pts/2 00:00:02 /usr/lib/firefox/firefox -contentproc -childID 1 -isForBrowser -prefsLen 1 -prefMapSize 231763 -parentBuildID 20210222142601 -appdir /usr/lib/firefox/browser 757284 true tab
glenn 757404 757284 3 08:37 pts/2 00:00:00 /usr/lib/firefox/firefox -contentproc -childID 2 -isForBrowser -prefsLen 229 -prefMapSize 231763 -parentBuildID 20210222142601 -appdir /usr/lib/firefox/browser 757284 true tab
glenn 757434 757284 6 08:37 pts/2 00:00:00 /usr/lib/firefox/firefox -contentproc -childID 3 -isForBrowser -prefsLen 6227 -prefMapSize 231763 -parentBuildID 20210222142601 -appdir /usr/lib/firefox/browser 757284 true tab
glenn 757497 757284 3 08:37 pts/2 00:00:00 /usr/lib/firefox/firefox -contentproc -childID 4 -isForBrowser -prefsLen 7022 -prefMapSize 231763 -parentBuildID 20210222142601 -appdir /usr/lib/firefox/browser 757284 true tab
===

Note the multiple child processes created.
Here is the same when running as www-data:

===
# sudo -u www-data firefox --headless
*** You are running in headless mode.
[GFX1-]: glxtest: Unable to open a connection to the X server
[GFX1-]: glxtest: libEGL initialize failed
^C
===

And ps:

===
root 757844 357685 0 08:38 pts/3 00:00:00 sudo -u www-data firefox --headless
www-data 757849 757844 2 08:38 pts/3 00:00:00 /usr/lib/firefox/firefox --headless
===

I don't think this is a firefox issue. I tried the same on an openSUSE server using the wwwrun user which spawned the child processes fine.
There is some restriction on Ubuntu but no mention of anything in the journal logs during the run attempts.

Any suggestions on how to deal with this would be greatly appreciated!

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Solved by:
Manfred Hampl
Solved:
Last query:
Last reply:
Revision history for this message
Bernard Stafford (bernard010) said :
#1
Revision history for this message
Stan (sdragnev) said :
#2

I'm afraid this doesn't solve the problem. I know how to use firefox in headless mode.
The problem is the different behaviour between running it as a regular user vs www-data on a ubuntu server.

As a result the selenuim script works when ran as the regular user but not as www-data.
So far I've tracked down the problem to firefox's different behaviour across users and because this works on a different linux distro, my guess for now is that it's a ubuntu restriction around www-data.

Revision history for this message
Bernard Stafford (bernard010) said :
#3

"[GFX1-]: glxtest: libEGL initialize failed" From your original post.
Could this package help? https://packages.ubuntu.com/focal/libegl-dev libegl1 is included in the dev version.
Or https://packages.ubuntu.com/focal/libegl1

Revision history for this message
Stan (sdragnev) said :
#4

libEGL error still persists after installing libegl-dev.
That said it doesn't seem to be the cause of the issue because I get the same error when running headless firefox as a regular user (instead of www-data), and that runs as expected.

Revision history for this message
Best Manfred Hampl (m-hampl) said :
#5

If something works with one user, but not with another one, then the cause must be hidden in different user settings.
You should start checking things like:
Does www-data have an existing home directory (for storing firefox settings), with write access to this directory, etc.

Revision history for this message
Stan (sdragnev) said :
#6

Thanks Manfred Hampl, that solved my question.

Revision history for this message
Stan (sdragnev) said :
#7

You were right on the money, Manfred! I can't believe how simple that was.

I had an earlier error about /var/www/.cache not being writable by www-data which I fixed but it appears that firefox was quietly failing because it couldn't create it's .mozilla dir in www-data's home. Not even fully failing but continuing to half-run with no error output.

Guess I was expecting too much from a mainly GUI program in terms of error reporting.

Thank you!
I had a feeling it would be simple but didn't think it would be THAT simple.