Ubuntu – How to close application from terminal same way as exiting it properly

command line

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?

Best Answer

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