About five years ago I answered a question on Superuser.com — Can I close firefox browser tab or firefox browser from ubuntu terminal?
My answer to this question was to use wmctrl, a CLI program to interact with an X Window Manager.
For closing the browser gracefully, wmctrl should do the trick:
DISPLAY=:0.0 wmctrl -c "Firefox"
you may need to install it first with
sudo apt-get install wmctrl
see the wmctrl man page for more info
I had completely forgotten about answering that question until today when I was notified via e-mail that the answer received a comment.
it doesn’t really work. It closes window, but doesn’t quit app. That’s different things.https://superuser.com/users/79081/poige
Poige is right. My solution doesn’t terminate the firefox process. All my solution does is close a window with “Firefox” in the title. If there happens to be more than one Firefox window open, the the firefox process remains running.
Well, how about a loop? We can run wmctrl multiple times, until it exits with a non-zero exit code.
while wmctrl -c "Firefox"; do sleep 0.1; done
This loop would run repeatedly, each time closing a window with “Firefox” in the title. The loop stops when there are no more windows with “Firefox” in the title.
sleep command is in there to give the system time to process the window closure.
These commands run fine and dandy unless you have the configured preference in your browser to warn when closing multiple tabs.
wmctrl can’t do anything about this confirmation dialog. It doesn’t have the capability of reading dialog buttons or pressing them!
we could always run
killall firefox if our end goal is to simply terminate the firefox process.
This has a few downsides. The first is that Firefox may interpret this termination as a crash. Upon starting Firefox in the future, Firefox may present you with a message indicating that there was a problem with your last browser session.
This may or may not be a problem for you, depending on your needs. However, I want to offer a solution which offers the best of both worlds–
An programmatic way to close Firefox as if a human had gone through the steps to close Firefox.
So what is it that a human would do to close Firefox? They’d probably click the big “X” in the top right corner, followed by clicking “Close Tabs” on the confirmation dialog.
Can we do that same thing via the command line? Sure can!
I’m going to build upon ZyMOS’s script by adding a few lines which handle the potential for a close confirmation dialog.
This script makes use of the CLI utility, xdotool.
#!/bin/bash # 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 # Simulate an "enter" key press in the confirmation window. # This does the default action of pressing the "Close tabs" button. xdotool key --clearmodifiers Return
That should do it! When run, this script will send Firefox the “Ctrl+q” hotkey which tells firefox to quit. Firefox may prompt the user for confirmation to close all tabs and quit, at which point the script will activate the confirmation window and press enter.
If Firefox’s confirmation window doesn’t appear, the script simply exits.
I recommend saving this script as close-firefox.sh and copying it somewhere in your environment
PATH such as
~/.local/bin/. This way, you can simply run
close-firefox.sh in your terminal, and firefox will gracefully close straight away.
Let me know what you think!