LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-17-2011, 07:53 AM   #1
Tryum
LQ Newbie
 
Registered: Oct 2010
Distribution: Zenwalk
Posts: 19

Rep: Reputation: 0
Zenity + kill PID


Do you know the GNOME Panel applet that is terminating running applications after clicking on them with the cursor and confirming the action with a Yes/No dialogue?


I know about xkill, but it is not satisfying due to it has not confirmation dialogue.


Instead, I would like to use xprop and kill and zenity

Here's what I have accomplished thus far:

PID_NUMBER
Code:
xprop|grep "PID"|sed "s/[^0-9]//g"
APPLICATION_NAME
Code:
xprop|grep "WM_CLASS"|cut -d \" -f 2
It should do something like this:
Code:
zenity --question --text "Are you sure you want to terminate <APPLICATION_NAME>?"; kill pid <PID_NUMBER>
Any solutions?

Last edited by Tryum; 02-17-2011 at 08:04 AM.
 
Old 02-17-2011, 11:47 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Something like this?

Code:
#!/bin/bash

IFS=$'\n'

array=( $( xprop | sed -rn -e '/WM_CLASS/ s/^.*"([^"]*)"$/\1/p' -e '/PID/ s/[^[:digit:]]//gp' ) )

zenity --question --text "Are you sure you want to terminate ${array[1],,}?"

case $? in

     0) kill ${array[0]}
        exit $?
     ;;

     *) echo "Operation canceled."
        exit 3
     ;;

esac
Note that you can use a single sed command to do all the work, instead of multiple pipes.

The sed command above seems to reliably output the window name first, then the PID. I chose the last WM_CLASS field though instead of the one you used because, a) it's easier to filter with sed, and b) it seems to give a more reliable name. My browser window outputs a line of WM_CLASS(STRING) = "Navigator", "Iceweasel", for example.

I then stored the output in an array. Setting IFS to newline ensures that I get one array element per line of output, no matter what the formatting.

Finally we can use a simple exit code test to determine which action to take, since zenity exits with 0 when "yes" is selected, and 1 when "no" is selected. I also set the script to exit with whatever exit code kill exits with, or "3" if you select cancel.

There's only one more thing to mention. the two commas in "${array[1],,}" convert the contents of the variable to lowercase, but this feature is only available in version 4 of bash. Remove them if your shell is an older version and use something like
Code:
array[1]="$(echo ${array[1]} | tr [:upper:] [:lower:] )"
before running the zenity command.
 
Old 02-17-2011, 11:53 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Oh, and just because I can, here's a variation that replaces the complex sed regex with a simple grep and uses parameter substitution to filter out the desired parts of the string.

Code:
#!/bin/bash

IFS=$'\n'

array=( $( xprop | grep -e 'WM_CLASS' -e 'PID' ) )

array[0]=${array[0]//[^[:digit:]]}
array[1]=${array[1]%\"}
array[1]=${array[1]##*\"}

zenity --question --text "Are you sure you want to terminate ${array[1],,}?"

case $? in

     0) kill ${array[0]}
        exit $?
     ;;

     *) echo "Operation canceled"
        exit 3
     ;;

esac

Last edited by David the H.; 02-17-2011 at 11:59 AM. Reason: small modification
 
1 members found this post helpful.
Old 02-17-2011, 01:56 PM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Code:
window_pid=$(xprop | grep '_NET_WM_PID(CARDINAL) = [0-9]\+$' | sed 's/.* \([0-9]*\)$/\1/')

if [ "$window_pid" '=' '' ]; then exit 1; fi

zenity --question --title='Kill Application' \
	--text='Really kill PID '$window_pid'?' \
	--ok-label=Yes --cancel-label=No

if [ $? '!=' 0 ]; then exit 2; fi

kill $window_pid

exit 0

Last edited by MTK358; 02-17-2011 at 02:02 PM.
 
1 members found this post helpful.
Old 02-23-2011, 06:59 AM   #5
Tryum
LQ Newbie
 
Registered: Oct 2010
Distribution: Zenwalk
Posts: 19

Original Poster
Rep: Reputation: 0
Thank you, to both of you, so much
 
Old 02-23-2011, 07:33 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Mark the thread as solved.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What does kill -0 PID do? asihsh.nakul Linux - Newbie 16 11-14-2012 05:18 PM
kill < file (Feeding kill from file), PID, <, awk, grep jaffd Programming 4 04-09-2010 04:06 PM
kill pid elainelaw Linux - Newbie 1 04-07-2010 08:43 AM
What is the more easier way to check the pid and kill the pid cmx08 Linux - General 5 09-09-2008 10:57 PM
kill pid.... won't work with 'pid' variable given.. sachitha Programming 6 03-06-2006 07:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration