35

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.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
slybloty
  • 1,228
  • 3
  • 17
  • 38

6 Answers6

35

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
  • SIGTERM is the same as signal 15, hence --signal TERM, or just leave it out since SIGTERM is the default signal
  • wmctrl works with Unity and some others but it does not work with all window managers
  • wmctrl -c closes 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
Keith Cascio
  • 491
  • 1
  • 4
  • 6
  • 2
    I 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
  • This still makes me see 'didn't shut down corrently' – xtrinch May 12 '17 at 09:06
  • 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
13

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).

Baldrick
  • 9,497
  • 2
  • 17
  • 8
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
Fleshgrinder
  • 115
  • 6
1

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"
hanxue
  • 551
  • 2
  • 9
  • 22
0

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.

Blackle Mori
  • 206
  • 1
  • 8
  • 2
    This 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
    try 15 instead of 3, then – Blackle Mori Mar 13 '12 at 19:40
  • 6
    Actually, `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
-1

For Ubuntu just simply enter:

exit google-chrome

Anna
  • 71
  • 1
  • 1
  • 3