8

I dont want to kill browser process, I just want to shut down the browser from command.

1
  • From the Ubuntu terminal you can't close the tabs of Firefox, but you could close the tabs of Chrome browser.
    – Devid
    Apr 15, 2013 at 21:13

3 Answers 3

8

Option 1. wmctrl

wmctrl can query the window manager for information, and it can request that certain window management actions be taken. (wmctrl manual)

For closing firefox gracefully, wmctrl could do the trick--

wmctrl -c "Firefox" -x "Navigator.Firefox"

you may need to install it first with

sudo apt install wmctrl

Caveat #1

wmctrl -c only closes one window at a time. If you have multiple firefox windows open, you will need to run wmctrl -c multiple times.

Example Solution

while wmctrl -c 'Firefox' -x 'Firefox.Navigator'; do sleep 0.1; done

Caveat #2

Close tabs? confirmation window prompt

If browser.tabs.warnOnClose is set to true, Firefox will prevent the window from closing. This can be changed in about:config, but that is a compromise to your Firefox user experience which may not be worth the risk.

Option 2. xdotool

xdotool lets you programatically (or manually) simulate keyboard input and mouse activity, move and resize windows, etc. (xdotool man page)

ZyMOS was the first to suggest this solution. I'm simply building on their answer by adding additional logic to handle the possible appearance of the close confirmation window.

Example Script

#!/bin/bash
#
# TITLE: close-firefox.sh
#
# DESCRIPTION: Programmatically closes firefox in a way 
#              similar to how a human closes firefox.


# Find the window ID of a Firefox window.
FFWID=$(xdotool search --name "Mozilla Firefox" | head -1)

# use the Firefox window ID to activate that window
xdotool windowactivate --sync $FFWID

# Send the "Quit" hotkey "Ctrl + q" to Firefox.
# https://support.mozilla.org/en-US/kb/keyboard-shortcuts-perform-firefox-tasks-quickly
xdotool key --clearmodifiers ctrl+q

# Give the system a moment to process
sleep 0.1

# Find the window ID of the Firefox close confirmation window
CWID=$(xdotool search --name "close tabs")

# End the script if xdotool didn't detect a confirmation window
if [ $? -ne 0 ]; then
  exit 0
fi

# Activate the Confirmation window
xdotool windowactivate --sync $CWID

# Send an "Return" key press to the confirmation window.
# This does the default action of pressing the "Close tabs" button.
xdotool key --clearmodifiers Return
3
  • it doesn't really work. It closes window, but doesn't quit app. That's different things.
    – poige
    May 22, 2020 at 8:16
  • I only suggested SIGTERM because closing the window wasn't sufficient for you. I didn't know you wanted graceful. But please see my updated answer for graceful. May 25, 2020 at 5:24
  • Don't wag — "Quit" is always graceful. Updated answer isn't suggesting anything useful.
    – poige
    May 25, 2020 at 7:03
5

You cannot close a tab, but you can close the browser using one of these:

pkill firefox
killall firefox
2
  • 2
    Actually i dont want to kill process. I want close it normally then next time firefox open normally without restore option.
    – tetrapack
    Apr 15, 2013 at 19:17
  • 1
    ‘kill‘ by default terminates (sigterm) a process; one has to specify "-9" to "kill" the process. The sigterm signal can be caught by a running process and handled gracefully (depending on the program implementation); the sigkill signal can't be caught & is the "ungraceful" exit that often requires program "recovery" at startup.
    – michael
    Aug 14, 2018 at 11:31
4

If you wish to "entirely" close Firefox "gracefully", try using xdotool. By gracefully, i mean closing the program, not killing it. By entirely, i mean "quiting" Firefox, closing all windows, all at once. wmctrl will gracefully close a single Firefox window, but will not close all windows.

Create the following script:

#!/bin/bash

WID=`xdotool search "Mozilla Firefox" | head -1`
xdotool windowactivate --sync $WID
xdotool key --clearmodifiers ctrl+q

That should do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.