Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is it possible to use the command line (or python) to gradually shutdown a gui program?

For instance, would it be possible to gradually shutdown programs (such as pressing the X in the window, not killing the process), giving the user the ability to save unsaved files before the program shuts down (such as gedit?)

share|improve this question

5 Answers 5

It's possible using wmctrl (packaged in ubuntu), which sends signals to EWHM/NetWM-compatible (most) window managers.

Closing kate gracefully would be done with the command:

wmctrl -c kate
share|improve this answer

I think the word 'gracefully' might better describe the type of shut-down you're after?

And have you tried just killall processname or kill pidnumber? (where processname or pidnumber are replaced with the name or PID of the process you're trying to shut down)

By default, kill sends a TERM signal which the application may treat as a graceful shutdown request (though its technically up to the developer what they do with the TERM signal).

share|improve this answer

The shutdown command lets you specify a grace period as well as a shutdown message (of your choosing) to warn users to save their work.

share|improve this answer

It seems unlikely there's a mechanism for shutting down a single application apart from shooting it dead.

It's probably better to think of the application as a black box. We could kill it, but it is difficult to influence its internal operation in subtle ways.

And from outside, shutting down gracefully (saving files, etc) is very subtle.

share|improve this answer

If the program is one you are writing, the best way to do this is to capture signals from the operating system and specify in the program how you want the program to handle them. Then, you can use the (unfortunately named IMO) kill command to send the appropriate signal to the program so that it stops in the manner you want. (Unfortunately, I have never done this with Python, but it looks like you may be able to use os.kill to send the signal.) Of course, you could do the same thing if it's not a program you wrote, but you know (or find out, possibly by trial and error) that it handles a specific signal in a fashion you desire.

I don't have Ubuntu, but you may be able to get a list of signals available by typing man signal at the command line.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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