36

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.

2
  • 2
    What is wrong with kill?
    – Zoredache
    Mar 13, 2012 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. Mar 13, 2012 at 21:50

6 Answers 6

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
4
  • 2
    I assume I am using an old version of pkill as --oldest is not an option: using -o works.
    – Ken Sharp
    Feb 10, 2015 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. Mar 4, 2015 at 17:13
  • This still makes me see 'didn't shut down corrently'
    – xtrinch
    May 12, 2017 at 9:06
  • In Ubuntu 18.04, how could I make this to be executed immediately after triggering a restart or a shutdown?. May 25, 2019 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).

3
  • 1
    This option works flawless, but it required the installation of wmctrl.
    – slybloty
    Mar 14, 2012 at 0:06
  • wmctrl cannot close system tray "Chrome Apps". pkill -o chrome does though.
    – Ken Sharp
    Feb 10, 2015 at 16:00
  • 1
    Only works on X Window Manager. Not a future proof solution
    – BrnVrn
    Sep 6, 2022 at 6:32
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
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"
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.

4
  • 2
    This acts just like kill or killall where Chrome sees it as a crash, and asking to restore.
    – slybloty
    Mar 13, 2012 at 18:54
  • 1
    try 15 instead of 3, then Mar 13, 2012 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, 2012 at 20:29
  • 2
    @blacklemon67 kill -15 <pid_of_chrome> 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, 2012 at 0:26
-1

For Ubuntu just simply enter:

exit google-chrome

1
  • 1
    The exit verb takes an optional status value, not a command Oct 8, 2020 at 8:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .