06 January 2013

LXDM PostLogout script to shutdown user's apps

LXDM is a bit strange in it's behaviour to not to kill your apps running when you log out. After logging out my deadbeef continues to play music and conky is still drawn on top of LXDM greeter. Look funny heh :)
I don't really like this, but I wanted DM to show list of users instead of simple prompt. Arch wiki suggests just to killall --user $USER which would also kill all your background processes they may be running (torrent, screen, etc).
Enough words - here goes the script:
$ cat /etc/lxdm/PostLogout 
#!/bin/sh
#
# Note: this is a sample and will not be run as is.

current_windows=`xlsclients -al | grep Window | cut -d' ' -f2 | sed -e 's/:$//'`
for wid in $current_windows
do
        pid=`xprop -id ${wid} _NET_WM_PID | awk -F ' = ' '{ print $2 }'`
        wmname=`xprop -id ${wid} WM_CLASS | cut -d= -f2 | cut -d, -f2 | sed -e 's/[ "]//g'`
        if [ "p${pid}" != "p" ]
        then
                echo "Killing ${wmname} (${pid})"
                kill ${pid}
        else
                echo "No PID found for ${wmname}"
        fi
done

# for some reason PID for conky cannot be determined
killall --user $USER conky
It requires xlsclients and xprop. Some apps (like conky) are special case and they don't show their PID in X11 properties of a window, so they are killed separately.

2 comments:

Anonymous said...

Nice script - I will try it. I personally use:
#!/bin/sh

# Kills all your processes when you log out.
killall conky
ps --user $USER | awk 'NR > 1 {print $1}' | xargs -t kill

# Sets the desktop background to solid black. Useful if you have multiple monitors.
xsetroot -solid black

which seems to be much more simply and requires only xargs

Kirill Malyshev said...

the reason for all that mess with xlsclients is to avoid killing any background apps that may be running. like transmission daemon for example or smth like that

if you have neither of those you may live with ps --user ..| xargs kill