14

I'm doing some automation with Firefox and although I can open Firefox window from shell, I'm not able to terminate it properly. If I kill Firefox process with kill -3 or kill -2 command the next time I open a new Firefox window it asks me if I want to run in safe-mode. I understand that calling kill -9 could confuse Firefox so it would try to run in safe-mode but -3 should be fine.

Do you have any idea how to gently tell Firefox to close properly?

scai
  • 20,297
  • 4
  • 56
  • 72
martin
  • 93,354
  • 25
  • 191
  • 226

8 Answers8

18

You can use pkill with the process name:

pkill -f firefox
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 2
    I did try `pkill`. It doesn't show that safe-mode dialogue window but it still thinks that all previously opened tabs crashed. – martin Oct 22 '13 at 11:22
  • 1
    See this answer: http://support.mozilla.org/en-US/questions/752748 It says `it looks like firefox shuts down cleanly on SIGTERM, so if you just `pkill firefox`, that should do it. if multiple instances are running and you only want to shut down one instance, you can use something like `pgrep -f [PROFILE] | xargs kill`.` – anubhava Oct 22 '13 at 11:29
  • 1
    Yes, it looks like it terminates properly but it doesn't. Maybe this has changed in more recent versions of Firefox. That thread is from 2010. Maybe it's some OS X specific problem. – martin Oct 22 '13 at 12:07
  • 1
    Hmm it could be. I have OSX, let me try some commands myself. – anubhava Oct 22 '13 at 12:28
8

How about

wmctrl -c "Mozilla Firefox"

?

Is it what you want?

NOTEs:

  1. This command may need to be fired in same DISPLAY & probably same virtual desktop, on which your firefox is running.
  2. Only first matching window will be closed. You may need to loop this command.
anishsane
  • 20,270
  • 5
  • 40
  • 73
  • 1
    I don't have any `wmctrl` on OSX – anubhava Oct 22 '13 at 12:28
  • ok... this package is for linux flavors. but there may be some alternate tool for similar purpose for OSX. Apparently, [it does not compile as-is for OSX](http://superuser.com/questions/496633/trouble-installing-wmctrl). – anishsane Oct 22 '13 at 12:56
  • `wmctrl` is for X Windows. While X Windows is available for Mac OS X, the primary GUI is not based on it. – chepner Oct 22 '13 at 13:40
  • This is the only method that will close Firefox properly under Ubuntu. I say Firefox because it's easy to see its result when it's not closed properly : the restore page. – sinekonata Apr 17 '14 at 20:27
3

In Mac OS X, you could use AppleScript to close it (adjust the application name as necessary; I don't have FireFox installed to test):

$ osascript -e 'tell application "FireFox"
quit
end tell'

This should trigger the same event that the Quit menu command triggers, so FireFox should shut down cleanly.

Obviously, this won't work in Linux.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • 2
    Nice, this works as expected. Firefox closes and reopenes fine. I was hoping for some cross-platform solution but if there was no other way, I'll use this. – martin Oct 22 '13 at 13:43
  • See my comment to your question; they're wild guesses, so I didn't add them to the answer. – chepner Oct 22 '13 at 13:46
2

You can first get the Pid of firefox with

pgrep firefox

and after use kill to stop firefox kill [pid]

kill `pgrep firefox`
Victor
  • 116
  • 1
  • 5
2

A bit more gentle

As @skybert indicated in the comment, you can

set browser.sessionstore.resume_from_crash to false.

in about:config, which

will rid you of the "Ups, something went wrong ..." message.

To get rid of the safe-mode message, you can set the config

toolkit.startup.max_resumed_crashes to -1

or (just set) the environment variable MOZ_DISABLE_AUTO_SAFE_MODE.

Less gentle

In lack of a better solution, you can remove sessionstore.js from the profile folder after the killall.

This is no more "gentle" than your solution, but it fixes the "Safe Mode" message:

Source: https://support.mozilla.org/en-US/questions/817752

Community
  • 1
  • 1
serv-inc
  • 35,772
  • 9
  • 166
  • 188
  • 1
    There is a more gentle solution, you can enter the `about:config` in Firefox and set `browser.sessionstore.resume_from_crash` to `false`. This will rid you of the "Ups, something went wrong ..." message. – skybert Nov 18 '15 at 09:45
  • @skybert: Thanks, the comment has been merged. – serv-inc Nov 18 '15 at 11:10
1

For Firefox in OSX:

killall 'firefox'
Ahmed Lotfy
  • 3,806
  • 26
  • 28
0

First Run xkill command after that click on window which one you want close

    xkill
Deepak Singh
  • 460
  • 2
  • 7
  • 19
0

You can close the FF by kill and PID

#!/bin/bash

firefox & PID=$! # Start Firefox and get PID for later

Running the FF some time

kill -15 $PID # Soft Kill of FF