Is there a way to cause google-chrome to quit, from the terminal, besides using killall google-chrome?
I would like to be able to close it from a script without killing it.
- 807,993
- 194
- 1,674
- 2,175
- 1,228
- 3
- 17
- 38
-
2What is wrong with kill? – Zoredache Mar 13 '12 at 19:26
-
1@Zoredache That it results in different behavior from exiting, and that Chrome uses multiple processes, one of which (the sandbox) is setuid root. – Gilles 'SO- stop being evil' Mar 13 '12 at 21:50
6 Answers
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
Details:
- 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
SIGTERM - signal a single process, the root of chrome’s process tree, hence
--oldest SIGTERMis the same as signal15, hence--signal TERM, or just leave it out sinceSIGTERMis the default signalwmctrlworks with Unity and some others but it does not work with all window managerswmctrl -ccloses one window at a time so to close all chrome windows you would need something likewhile wmctrl -c 'Google Chrome'; do sleep 0.2; done
- 491
- 1
- 4
- 6
-
2I assume I am using an old version of pkill as `--oldest` is not an option: using `-o` works. – Ken Sharp Feb 10 '15 at 16:01
-
the `-f` argument to pkill is not working for me. I have some other processes running as root that happen to have the word chrome in their command lines, so I just get permission denied. Without the `-f`, it works perfectly though. – Brian Minton Mar 04 '15 at 17:13
-
-
In Ubuntu 18.04, how could I make this to be executed immediately after triggering a restart or a shutdown?. – Jaime Hablutzel May 25 '19 at 19:12
Perhaps wmctrl could be of some assistance. You could use the -c option that closes a window gracefully:
wmctrl -c chrome
The string 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).
- 103
- 4
- 9,497
- 2
- 17
- 8
-
1This option works flawless, but it required the installation of `wmctrl`. – slybloty Mar 14 '12 at 00:06
-
wmctrl cannot close system tray "Chrome Apps". `pkill -o chrome` does though. – Ken Sharp Feb 10 '15 at 16:00
-
1
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
- 115
- 6
On 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"
try:
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.
- 206
- 1
- 8
-
2This acts just like `kill` or `killall` where Chrome sees it as a crash, and asking to restore. – slybloty Mar 13 '12 at 18:54
-
1
-
6Actually, `SIGQUIT` is not usually sent to applications when asked to close (I don't know any WMs that do this). `WM_DELETE_WINDOW` is the standard. – Chris Down Mar 13 '12 at 20:29
-
2@blacklemon67 `kill -15
` did what I was looking for. But, `google-chrome` has multiple pids and it took a few tried to actually get the right one. – slybloty Mar 14 '12 at 00:26