How to close application from terminal same way as exiting it properly
The typical way to close application gracefully is to use kill -TERM 1234
, where 1234
is the PID of the window. Problem, however, is that graphical applications are designed to listen for a specific signal from X server itself , not listening for the TERM
signal.
That means you would need some kind of agent in between you and the X server to communicate that you want to send appropriate signal. You can of course write a C program, however there aready exists wmctrl
program ( not-surprisingly written in C ) that does exactly that.
Usage of wmctrl
is simple:
wmctrl -c :SELECT: # close window that will be selected with pointer
wmctrl -c :ACTIVE: # close currently active window
wmctrl -ic <numeric id> # close window with specific id in hex
It's not installed by default, so get it with sudo apt-get install wmctrl
There's many uses for wmctrl
is far beyond just closing windows, so please see more of my answers and Jacob Vlijm's for examples of scripts where it's been used
I think it's worth adding that wmctrl -xl
will output results like this:
0x03000001 -1 google-chrome.google-chrome dcentore-pc Rybak - Google Play Music - Google Chrome
0x04600006 0 speedcrunch.Speedcrunch dcentore-pc SpeedCrunch
Where the x
requests the window class to be in the output. This is often the easiest way to close the same program every time. Then, we can just supply the window class when closing it:
wmctrl -xc speedcrunch.Speedcrunch
This example will close speedcrunch.