2

I want to quit Firefox from the command line, without "forcing" it. I just want to cause the same as clicking Quit in the menu in Firefox would.

Seems I need to call "goQuitApplication" somehow, based on the following.

I tried making a xul file and running that, but I got an error in it. I tried running the file from this Mozillazine post, but Firefox said in the console that "TypeError: Components.classes is undefined".

The following is the code of that xul file:

Remote XUL Manager by Jorge Villalobos
https://addons.mozilla.org/firefox/addon/235281/

Domains with remote XUL permission: <Local files>

Loads the specified chrome:
firefox -chrome example.xul

-->

<window onload="load()"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

  <script type="application/javascript">
  <![CDATA[

    function load() {
      var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].
        getService(Components.interfaces.nsIWindowMediator);
      var win = wm.getMostRecentWindow("navigator:browser");
      win.setTimeout("goQuitApplication()", 250);
      window.close();
    }

  ]]>
  </script>

</window>

I've tried simply calling "goQuitApplication" inside the load function, (which seemingly works if called inside the browser console) but then I got the error "ReferenceError: goQuitApplication is not defined".

Does anyone know of a good way to quit Firefox from the command line, without force killing the process or closing individual windows?

6
  • Have you checked whether Taskkill or its Powershell equivalent, Stop-Process, were suited for your needs? May 1, 2014 at 17:37
  • when i call TASKKILL /im firefox.exe, firefox seems to act as if i had closed the most recently used firefox window (asking me if i really want to close <# of tabs open in that window>), instead of just quitting.
    – Matthias
    May 1, 2014 at 18:56
  • 1
    That's expected behavior of a default Firefox installation. You can always disable that confirmation, in the Options > Tabs - "Warn me when closing multiple tabs". May 1, 2014 at 19:41
  • yes, i realize that. the problem is that i use multiple windows, and this only closes one of them. also, the tabs are then considered "closed", so they wont come back next time you open firefox.
    – Matthias
    May 1, 2014 at 20:25
  • Asking that question is what Firefox likes to do. If you don't want Firefox to ask that question, then you're trying to force Firefox to close without performing its standard behavior. However, in this SuperUser.com question, you specifically said "without forcing". So you need to make up your mind: do you want to force, or not?
    – TOOGAM
    Feb 6, 2015 at 15:17

1 Answer 1

2

I actually ran into this question yesterday. I was looking for the answer myself. Not a big deal if you only have one windows open (a simple taskkill line without the /f so that the browser can close cleanly) but as soon as you have two windows or more, it becomes alot more complicate because the taskkill command will only shot one window at a time (effective loosing the tabs of the window unless it's the last one to be closed).

I ended up writing a combo of cmd/vbs scripts to close firefox cleanly. Scripts will detect if Master Password prompt is open and close it (avoid the prompt from interfering with FFQuit script), FFQuit script will then activate FF and send an Alt-F key stroke to open file menu and an X to exit firefox.

There you have it a quick and clean exit, all windows come back upon restart. There's the option to restart FF as well (just uncoment the relevant section):

Part 1: the encapsulating cmd script: quitFF.cmd

set FFexec=%programfiles(x86)%\Mozilla Firefox\firefox.exe
set ERRORLEVEL=

:: Check if that annoying Master Password prompt is open and close it
tasklist /FI "WINDOWTITLE eq Password Required" 2>NUL | find /I /N firefox.exe">NUL
IF "%ERRORLEVEL%"=="0" MPWDclose.vbs

FFQuit.vbs

::Optional Restart Firefox - ping is just a sneaky way of getting sleep functionality
::Uncomment REM to activate
REM ping 1.0.0.0 -n 1 -w 2000 > nul
REM start ""  "%FFexec%" & exit

Part 2: MPWDclose.vbs

Set MPWDclose = WScript.CreateObject("WScript.Shell")

WScript.Sleep 250
MPWDclose.AppActivate("Password Required")
WScript.Sleep 250
MPWDclose.Sendkeys "{ESC}"
WScript.Sleep 50
WScript.Quit

Part 3: FFQuit.vbs

Set FFquit = WScript.CreateObject("WScript.Shell")
WScript.Sleep 250
FFquit.AppActivate("- Mozilla Firefox")
WScript.Sleep 250
FFquit.Sendkeys "%F"
WScript.Sleep 100 
FFquit.Sendkeys "x"
WScript.Quit
4
  • I know it's been a couple of years, but thanks for this. I don't have Master Password enabled, so all I actually needed was that VBS file. But it works beautifully. Saved me a bunch of headache.
    – spoko
    Mar 14, 2018 at 15:41
  • It's pretty neat that something I put up a few years ago is helping someone today. Thanks for the feedback. :) Mar 14, 2018 at 17:16
  • 1
    Gotta love the timelessness of the interwebs.
    – spoko
    Mar 14, 2018 at 18:23
  • I just configure my browsers to save and restore all windows and tabs and terminate cleanly instead of showing dialog boxes that block OS requests to end the process gracefully.
    – svin83
    Mar 14, 2023 at 23:08

You must log in to answer this question.

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