Is there a way to cause
google-chrome to quit, from the terminal, besides using
I would like to be able to close it from a script without killing it.
wmctrl could be of some assistance. You could use the
-c option that closes a window gracefully:
wmctrl -c chrome
chrome is matched against the window titles. Note that the window might not close if some message pops-up (e.g. when you have multiple tabs open).
This command exits the chrome process tree gracefully, in all window managers:
pkill --oldest chrome
or if you prefer:
/usr/bin/pkill --oldest --signal TERM -f chrome
- gracefully means: avoid seeing “Google Chrome didn't shut down correctly. To repoen ...” next time chrome starts
- chrome browser (e.g. version 39.0.2171.95) traps and gracefully handles
- signal a single process, the root of chrome’s process tree, hence
SIGTERMis the same as signal
--signal TERM, or just leave it out since
SIGTERMis the default signal
wmctrlworks with Unity and some others but it does not work with all window managers
wmctrl -ccloses one window at a time so to close all chrome windows you would need something like
while wmctrl -c 'Google Chrome'; do sleep 0.2; done
This works for me:
killall --quiet --signal 15 -- chrome
Note that I'm using a rather verbose command to keep it readable in the code, of course you could also issue:
killall -q -15 chrome
Mac OS X, use this instead
pkill -a -i "Google Chrome"
What it does is to look for a Google Chrome process, and kill all of its parent processes as well.
From the pkill manual
-a Include process ancestors in the match list. By default, the current pgrep or pkill process and all of its ancestors are excluded (unless -v is used). -i Ignore case distinctions in both the process table and the supplied pattern.
As per @keith-cascio ' s answer, you can try to kill the oldest process instead. Note that this did not work for me.
pkill -o -i "Google Chrome"
kill -3 <pid_of_chrome>
This will send a "QUIT" signal to chrome, which, depending on your window manager, will be what it is usually sent when asked to close.