3

I am playing with selenium in Python and Firefox windows in headless mode. The problem is that I've created lots of Firefox windows in headless mode using this useful answer, if you ask me.

First of all, I don't understand why this answer earned no votes whatsoever, except for my own, even if it seems to work fine. Can someone explain? Maybe I am missing something. ¯\_(ツ)_/¯

Secondly, unfortunately, I did not close those windows running in headless mode using driver.close() in Python code, and there are lots of them. I wonder, how can I close them all now?

The only solution I have found (for Ubuntu and MacOS High Sierra) is this:

ps aux | grep firefox | awk '{print $2}' | xargs kill

..., which should be run in terminal.

But it not only closes all Firefox windows in headless mode. It literally kills all the processes related to Firefox.

ibodi
  • 1,543
  • 3
  • 21
  • 40

2 Answers2

1

Search by geckodriver and then kill your gecko processes.

netstat -tlp | egrep '(firefo|vnc|gecko)'

It comes something like this:

0      0 localhost:9090          *:*                     LISTEN      11970/geckodriver
SteroidKing666
  • 553
  • 5
  • 13
  • Currently I don't have access to `Ubuntu`. I am trying this on `MacOS High Sierra`, and it is not working there. My "solution", however, works for both `Ubuntu` and `MacOS High Sierra`. ¯\\_(ツ)_/¯ – ibodi Aug 07 '18 at 18:58
  • I am trying it out in Ubuntu. I am not sure how `egrep` works. Could you explain? `netstat -tlp | grep firefox` gives me info about the processes in headless mode, indeed. How did you find it out? And also, I don't know how to extract the PID from the line :( – ibodi Aug 08 '18 at 07:30
  • 1
    egrep treats the pattern as a regular expression. Also, netstat -tlp | egrep '(firefo|chro|gecko)' | awk '/:* */ {split($NF,a,"/"); print a[2],a[1]}' .... this can help you extract the PID. – SteroidKing666 Aug 08 '18 at 08:06
1

When I tried the accepted answer, it killed some visible firefox windows as well. Using htop, I found that all firefox windows running in headless mode using python selenium corresponded to the command /usr/lib/firefox/firefox --marionette --headless -foreground -no-remote -profile <tmp file>. So, using this answer on Unix stack exchange, I came up with:

for pid in $(ps -ef | grep "firefox --marionette -headless" | awk '{print $2}'); do kill -9 $pid; done`
mansi
  • 33
  • 1
  • 5