9

Usually when running application in graphical environment, when I want to close it I click X in the top left corner of the window or go to File > Close.

I would like to sometimes close these programs remotely over ssh from terminal using command line, now I use kill command but I wonder it it does the same as exiting the application properly from itself.

From man kill default signal appears to be 15) SIGTERM, is this signal going to tell the application I am trying to close to clean up, close open files, save stuff?

Is there a better way if I want to do it from terminal?

  • 2
    Thanks for asking! I learned something that will surely come in handy at some point. – Oliphaunt Jun 9 '16 at 19:59
9

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

  • 2
    Just adding on here that wmctrl -l will list all the windows and give the numeric id in hex in the first column. =) – Terrance Jun 9 '16 at 17:21
  • Thank you, works great with most of the applications, but with Deluge there is an option Minimize to tray on close and wmctrl -c will just close a window, not the application itself, it will stay running minimized to tray. – Mike Jun 9 '16 at 22:36
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.