LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Red Hat (https://www.linuxquestions.org/questions/red-hat-31/)
-   -   how to close "any" application cleanly from command line (https://www.linuxquestions.org/questions/red-hat-31/how-to-close-any-application-cleanly-from-command-line-887974/)

pponnath 06-23-2011 01:42 PM

how to close "any" application cleanly from command line
 
My Redhat release: CentOS release 5.3 (Final)
Question:
Sometimes i open so many 'nedit'(editor application) windows and i have to close all of them one by one. I want to close all of them from the command line. But i donot want to use 'kill' since that will forcibly terminate without saving the file(if i want to).
Is there any way we can run from command line to close an application but prompting me to save if it is not saved.
In short, i want a command line equivalent of 'moving the mouse and clicking 'X' on the window

bhaslinux 06-23-2011 02:25 PM

xprop gives the PID of that process

you can send a HUP to that PID. But the process of exiting an application gracefully is totally application dependent and is handled by each application in its own manner. Many do not support HUP signals and it will just crash the program. Some others will respond to USR1 signal.

_NET_WM_PID is the xprop which gives the PID of that running process

colucix 06-23-2011 03:21 PM

You can try wmctrl. It can send a graceful close command to X applications. The only problem is that it select open X applications based on the window title, not on the application name. Luckily you can modify the appearance of the windows title in the Nedit preferences, so that a uniquely identified string can be a valid fingerprint for the application you want to close.

In Nedit go to Preferences --> Default Settings --> Customize Window Title. In the Format box you can write any string. For example if you choose:
Code:

Nedit: {%c} [%s] %f (%S) - %d
it will show the default window title preceded by the string Nedit:. Save the defaults to make this behavior permanent.

Now to quit a nedit X window you can do
Code:

wmctrl -c Nedit
Unfortunately wmctrl does not have a recursive option, so that it can close only one window. If you want to close multiple nedit session at once, you can define a function like this:
Code:

xquit ()
{
    for n in $(wmctrl -l | awk -v name=$1 '$0 ~ name{print $1}');
    do
        wmctrl -i -c $n;
    done
}

this will loop over all the windows having a specific title. The command:
Code:

xquit Nedit
now will close all the sessions and will ask to save modified files (if any). Hope this helps! :jawa:

pponnath 06-23-2011 04:19 PM

Thanks for the reply.

I donot find wmctrl as part of redhat release that i have.

colucix 06-23-2011 04:44 PM

It is provided by the RPMforge repository (direct download here).

John VV 06-23-2011 06:18 PM

you can use < ctlr> + <c>
that will kill the current running command


All times are GMT -5. The time now is 03:54 PM.