You are not logged in.

#1 2009-03-29 02:26:32

liquidsunshine
Member
Registered: 2008-07-30
Posts: 7

[Solved] Remotely closing X apps over ssh

I'm trying to figure out a way to remotely close an application (like firefox) cleanly over ssh.  I know I could run "kill [pid]" or "killall firefox-bin" to close it, but the default termination signal causes firefox to close immediately and give a message at its next start complaining about the program not being shut down properly.  I tried several other signals, all of which had the same problem. 

Is there a signal that I can issue from kill that will be equivalent to closing the application by clicking the X, pressing Alt-F4, going to File > Quit, etc?  If not, is there some other command I can issue to accomplish the same goal?

Last edited by liquidsunshine (2009-03-30 21:56:43)

Offline

#2 2009-03-30 13:58:32

dunz0r
Member
From: Sweden
Registered: 2009-03-30
Posts: 258
Website

Re: [Solved] Remotely closing X apps over ssh

man kill should do it


RTFM or GTFO
hax0r.se

Offline

#3 2009-03-30 14:18:05

EVRAMP
Member
From: Czech Republic
Registered: 2008-10-03
Posts: 173
Website

Re: [Solved] Remotely closing X apps over ssh

I'm a happy user of pkill command (process kill).

Offline

#4 2009-03-30 16:08:22

Bralkein
Member
Registered: 2004-10-26
Posts: 354

Re: [Solved] Remotely closing X apps over ssh

Hi, this is an interesting problem! I tried to close firefox using many different signals, but all of the ones that closed firefox made it issue the warning about an unexpected shutdown, which is not what you wanted.

Secondly, I tried to see if there is a way to use IPC methods to shut down firefox. Many programs can be controlled via e.g. dbus. You can browse these applications and the functionality exposed through dbus using something like qdbusviewer (probably part of Qt). Unfortunately I did not find a way to close firefox using this or any similar method.

Finally I found a program in the community repo called wmctrl. If you do wmctrl -l you can get a list of apps being managed by the WM:

[jack@Jackington ~]$ wmctrl -l
0x01a002e8 -1 Jackington Qt-subapplication
0x01a00280 -1 Jackington Qt-subapplication
0x03600047  0 Jackington jack : bash
0x03800095  0 Jackington Arch Linux Forums / Post a reply - Gran Paradiso
0x02e00047 -1 Jackington Yakuake

To close firefox I used:

[jack@Jackington ~]$ wmctrl -i -c 0x03800095

and that worked. Perhaps there is a prettier way to do it without using that big hex number, but you will have to read the wmctrl man page yourself!

Hope this helps.

PS I tried this all using a KDE/KWin environment. Maybe it will work differently with whatever you use. The wmctrl man page said it works with a EWMH/NetWM compatible X Window Manager, whatever that means.

Last edited by Bralkein (2009-03-30 16:11:06)

Offline

#5 2009-03-30 17:03:06

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [Solved] Remotely closing X apps over ssh

@Bralkein, i threw together this bash script to let you search for and close a process by name.  firefox is a bad example as it looks like you'd have to search for "gran paradiso" although my grep -i makes it case insensitive at least.  anyways, i haven't tested it but it should work.

#!/bin/bash
# wKill
# pbrisbin 2009
#
# gracefully closes a programs window from
# a remote ssh session

# set DISPLAY to your remote X session
export DISPLAY=":0.0"

# prepare the search string
search=$(echo "$@" | sed 's/\ /\\\ /g')

# get the apps hex value
app=$(wmctrl -l | grep -i "$search" | awk '{print $1}') || (echo "no app found" && exit 1)

# close the apps window
wmctrl -i -c $app || echo "app not killed?"

# put the DISPLAY back to prevent any problems
export DISPLAY=""

exit 0

hmm... well seems this doesn't help the OP at all since wmctrl returns "can't open display" when run via ssh

EDIT: fixed for the OP, export DISPLAY as your remote X display, then wmctrl works and can be used to close windows (among other things)... one exception case would be when you've got multiple things open that will return those hex values, not sure how to handle that yet.

oh well i like the code anway.

usage: wkill gran paradiso

EDIT2: whoops, just tested: not behaving very well, hehe.  maybe version 2.0 to come soon?

EDIT3: ok... the above should work just fine smile

Last edited by brisbin33 (2009-03-30 17:18:59)

Offline

#6 2009-03-30 17:52:52

Bralkein
Member
Registered: 2004-10-26
Posts: 354

Re: [Solved] Remotely closing X apps over ssh

Cool, good idea. One can also tell wmctrl to display the PIDs which own the windows by using wmctrl -pl. It is then possible to close a window if you know the controlling process name using this hideous one-liner I just concocted, eg for firefox:

[jack@Jackington ~]$ wmctrl -i -c `wmctrl -pl | grep $(pgrep firefox) | awk '{print $1}'`

A bit horrible but hey! Only problem is I don't think all windows are necessarily capable of reporting a correct owner PID in which case the value listed by wmctrl is zero. I can't find an app this doesn't work with, though.

Offline

#7 2009-03-30 19:29:21

Army
Member
Registered: 2007-12-07
Posts: 1,784

Re: [Solved] Remotely closing X apps over ssh

This is really amazing, I asked myself this question just a few minutes ago and now checking the new stuff on bb.archlinu.org gives me the solution. Do you guys read other's minds? big_smile
brisbin33, your script works here, thank a lot for it! I'm on Openbox

Last edited by Army (2009-03-30 19:30:01)

Offline

#8 2009-03-30 21:55:09

liquidsunshine
Member
Registered: 2008-07-30
Posts: 7

Re: [Solved] Remotely closing X apps over ssh

Thank you all for your responses. 

@Bralkein:
Thank you for exploring this further.  Wmctrl is exactly the type of tool that I was looking for!  And the "hideous one-liner" does the trick nicely.

@brisbin33:
Your script works great.  Only thing I might change for my own usage is to use the process name like Bralkein's one-liner since I can never remember that Firefox is "Gran Paradiso" on Arch.  Great job putting it together though. 

Again, thanks everyone!

Note: I use Gnome on the machine I was ssh-ing into, just to confirm that it works there.

Last edited by liquidsunshine (2009-03-30 21:58:19)

Offline

#9 2009-03-31 00:22:23

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [Solved] Remotely closing X apps over ssh

@Bralkein:
i like the one liner, i've incorporated it into my script.  i split it up only because i wanted more meaningful errors.

@liquidsunshine:
thanks for asking this question, this turned out a nifty script.  i use pkill all the time, now i have wkill to gracefully close programs from CLI and it works from SSH just as well as locally.

here's the finished product:

#!/bin/bash
# wKill
# pbrisbin 2009
#
# gracefully close a program's window from
# a remote ssh session, or not
#
# requires wmctrl
#
# example usage: wkill firefox
#
###

# always exit cleanly
errorout() {
  if [ $R -eq 1 ]; then
    export DISPLAY=""
  fi
  echo "$1"
  exit $2
}

# set DISPLAY to your remote X session if we're ssh'd
# though: maybe a "local DISPLAY=" would work?
# it'd be cleaner if it did
if [ -z "$DISPLAY" ]; then
  export DISPLAY=":0.0"
  R=1
else
  R=0
fi

# get the PID by process name
PID=$(pgrep "$1") || errorout "no process by the name: $1" 1

# get the hex by pid  
APP=$(wmctrl -pl | grep "$PID" | awk '{print $1}') 

# some things don't get reported correctly
if [ -z "$APP" ]; then
  APP=$(wmctrl -l | grep "$1" | awk '{print $1}')
  if [ -z "$APP" ]; then
    errorout "wmctrl can't find pid: $PID or app: $1" 1
  fi
fi

# close the apps window
wmctrl -i -c $APP || errorout "wmctrl could not close app: $APP" 1

# put the DISPLAY back to prevent any problems
# do this only if we set it above 
if [ $R -eq 1 ]; then
  export DISPLAY=""
fi

exit 0

as Bralkein said, some windows might not get reported correctly.  i added a nested if in there for two reasons:

a) anything running in a terminal (irssi, mutt, htop) will only be captured by this if they were started with the terminal.  something like `xterm -e mutt`.  if you opened xterm, then opened mutt.  it will be listed as "bash ~" or something in wmctrl and the pid would be of mutt and not xterm... so we're kinda SOL there

b) b/c i'd rather get a nice "could not find pid: X for app: Y" instead of a grep or wmctrl error message (which is what you'd get if the $APP variable is empty)

other than the CLI app issue, it should pretty much catch every exception and run quite cleanly.  now if only i knew a real programming language...

Last edited by brisbin33 (2009-03-31 00:26:49)

Offline

#10 2009-04-07 10:23:45

Army
Member
Registered: 2007-12-07
Posts: 1,784

Re: [Solved] Remotely closing X apps over ssh

How would a command line look, which closes all opened windows? I would love to integrate something like that into the script which shuts down or reboots my system, so I don't have to close them manually, because by default they are being killed, which is not very nice wink

Offline

#11 2009-04-07 12:09:59

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 2,001
Website

Re: [Solved] Remotely closing X apps over ssh

Army wrote:

shuts down or reboots my system, so I don't have to close them manually, because by default they are being killed, which is not very nice wink

actually by default kill uses sigterm, which allows applications to shutdown gracefully.
In fact I think all apps implement a sigterm signal the same as a "shutdown message" from the window manager.

So I think all the code here is not needed, and you don't need wmctrl unless you really want to specify an app by it's window properties (title etc) instead of process properties (command name, pid etc)


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

#12 2009-04-07 12:59:36

liquidsunshine
Member
Registered: 2008-07-30
Posts: 7

Re: [Solved] Remotely closing X apps over ssh

@Dieter@be:
I thought the same thing.  However, through experience (of me and others, read above), Firefox does not close gracefully when sent the SIGTERM signal.  When it is started again, it gives the same error message that it would give if you SIGKILL'd it.  Perhaps this is an oddity of Firefox, but either way it suggests that SIGTERM and a "close window" message from the window manager are not necessarily the same, even though they often may be. 
Using wmctrl is the only way I can get Firefox to close cleanly through a remote shell. 

@Army:
Good question.  I think this line of thinking could lead to a much cleaner shutdown sequence overall (from a GUI at least).  Instead of killing the processes, it would ask them to close themselves nicely through the WM, and if they had something left to do (gave a "do you want to save?" dialog, etc) it could pause the shutdown procedure until they were dealt with.  Something like (shutdown windows) && init 0...
I don't have time this moment to craft code for your question, but I know that "wmctrl -l" lists all of the open windows, so you could parse that with "awk" to get the unique identifier of each window and close it in turn.  I'll try to get back to this later with an actual string of code, but that should set you on the right track.

Offline

#13 2009-04-07 13:12:28

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [Solved] Remotely closing X apps over ssh

i actually implemented this right after posting my post above.  i have an openbox menu item for logoff...

#!/bin/bash
wmctrl -l | awk '{print $1}' | while read APP; do
  wmctrl -i -c $APP
done

pkill openbox

exit 0

Offline

Board footer

Powered by FluxBB