Locking KDE 5 screen from shell script

I recently installed kde5 on debian stretch. I have a yubikey which I use to log in with. I'd like to lock the screen when I remove the yubikey. I have it working with cinnamon and lxde but I can't quite get it with kde. I can lock the screen if I run the shell script from a terminal as root but when I remove the yubikey the shell script returns error code 1. Here's my shell scripts: ykgone.sh Konsole output #! /bin/sh if [ -z "$(lsusb | grep Yubikey)" ] ; then if [ ! -z "$(ps aux | grep cinnamon-session | grep -v grep)" ] ; then /bin/su rjonasz -c "DISPLAY=:0 cinnamon-screensaver-command -l" elif [ ! -z "$(ps aux | grep startkde | grep -v grep)" ] ; then /usr/bin/sudo -u rjonasz -H -i /usr/local/bin/lockkde.sh else /bin/su rjonasz -c "DISPLAY=:0 xscreensaver-command --lock" fi fi lockkde.sh Konsole output #! /bin/sh kde_pid=`pidof kdeinit5` export `cat /proc/$kde_pid/environ|grep -ao 'DBUS_SESSION_BUS_ADDRESS=[[:graph:]]*'` /usr/bin/qdbus org.freedesktop.ScreenSaver /ScreenSaver Lock I can't seem to figure this one out. Thanks for any help! Randy -- Even philosophers will praise war as ennobling mankind, forgetting the Greek who said: 'War is bad in that it begets more evil than it kills.' Immanuel Kant

You could change this line ps aux | grep cinnamon-session | grep -v grep to this pgrep cinnamon-session It's cleaner and works well. To answer the main question, try to install bashdb and run the script thru it. It's the bash debugger, it's far more powerful than bash -vx. On Aug 12, 2015 04:22, "Randy Jonasz" <rjonasz@rjonasz.org> wrote:
I recently installed kde5 on debian stretch. I have a yubikey which I use to log in with. I'd like to lock the screen when I remove the yubikey. I have it working with cinnamon and lxde but I can't quite get it with kde. I can lock the screen if I run the shell script from a terminal as root but when I remove the yubikey the shell script returns error code 1.
Here's my shell scripts:
ykgone.sh
#! /bin/sh if [ -z "$(lsusb | grep Yubikey)" ] ; then if [ ! -z "$(ps aux | grep cinnamon-session | grep -v grep)" ] ; then /bin/su rjonasz -c "DISPLAY=:0 cinnamon-screensaver-command -l" elif [ ! -z "$(ps aux | grep startkde | grep -v grep)" ] ; then /usr/bin/sudo -u rjonasz -H -i /usr/local/bin/lockkde.sh else /bin/su rjonasz -c "DISPLAY=:0 xscreensaver-command --lock" fi fi
lockkde.sh
#! /bin/sh
kde_pid=`pidof kdeinit5` export `cat /proc/$kde_pid/environ|grep -ao 'DBUS_SESSION_BUS_ADDRESS=[[:graph:]]*'` /usr/bin/qdbus org.freedesktop.ScreenSaver /ScreenSaver Lock
I can't seem to figure this one out.
Thanks for any help!
Randy
-- Even philosophers will praise war as ennobling mankind, forgetting the Greek who said: 'War is bad in that it begets more evil than it kills.'
Immanuel Kant
--- Talk Mailing List talk@gtalug.org http://gtalug.org/mailman/listinfo/talk

Thanks for the tip Mauro, Lennart! It turns out the problem was with not having the DISPLAY variable set properly. So in lockkde.sh I invoke qdbus this way Konsole output DISPLAY=:0 /usr/bin/qdbus org.freedesktop.ScreenSaver /ScreenSaver Lock And now it works! Thanks again, Randy On 12/08/15 07:35 AM, Mauro Souza wrote:
You could change this line ps aux | grep cinnamon-session | grep -v grep
to this pgrep cinnamon-session
It's cleaner and works well.
To answer the main question, try to install bashdb and run the script thru it. It's the bash debugger, it's far more powerful than bash -vx.
On Aug 12, 2015 04:22, "Randy Jonasz" <rjonasz@rjonasz.org <mailto:rjonasz@rjonasz.org>> wrote:
I recently installed kde5 on debian stretch. I have a yubikey which I use to log in with. I'd like to lock the screen when I remove the yubikey. I have it working with cinnamon and lxde but I can't quite get it with kde. I can lock the screen if I run the shell script from a terminal as root but when I remove the yubikey the shell script returns error code 1.
Here's my shell scripts:
ykgone.sh
#! /bin/sh if [ -z "$(lsusb | grep Yubikey)" ] ; then if [ ! -z "$(ps aux | grep cinnamon-session | grep -v grep)" ] ; then /bin/su rjonasz -c "DISPLAY=:0 cinnamon-screensaver-command -l" elif [ ! -z "$(ps aux | grep startkde | grep -v grep)" ] ; then /usr/bin/sudo -u rjonasz -H -i /usr/local/bin/lockkde.sh else /bin/su rjonasz -c "DISPLAY=:0 xscreensaver-command --lock" fi fi
lockkde.sh
#! /bin/sh
kde_pid=`pidof kdeinit5` export `cat /proc/$kde_pid/environ|grep -ao 'DBUS_SESSION_BUS_ADDRESS=[[:graph:]]*'` /usr/bin/qdbus org.freedesktop.ScreenSaver /ScreenSaver Lock
I can't seem to figure this one out.
Thanks for any help!
Randy
-- Even philosophers will praise war as ennobling mankind, forgetting the Greek who said: 'War is bad in that it begets more evil than it kills.'
Immanuel Kant
--- Talk Mailing List talk@gtalug.org <mailto:talk@gtalug.org> http://gtalug.org/mailman/listinfo/talk
--- Talk Mailing List talk@gtalug.org http://gtalug.org/mailman/listinfo/talk
-- Even philosophers will praise war as ennobling mankind, forgetting the Greek who said: 'War is bad in that it begets more evil than it kills.' Immanuel Kant

On Wed, Aug 12, 2015 at 03:21:41AM -0400, Randy Jonasz wrote:
I recently installed kde5 on debian stretch. I have a yubikey which I use to log in with. I'd like to lock the screen when I remove the yubikey. I have it working with cinnamon and lxde but I can't quite get it with kde. I can lock the screen if I run the shell script from a terminal as root but when I remove the yubikey the shell script returns error code 1.
Here's my shell scripts:
ykgone.sh
Konsole output #! /bin/sh if [ -z "$(lsusb | grep Yubikey)" ] ; then if [ ! -z "$(ps aux | grep cinnamon-session | grep -v grep)" ] ; then /bin/su rjonasz -c "DISPLAY=:0 cinnamon-screensaver-command -l" elif [ ! -z "$(ps aux | grep startkde | grep -v grep)" ] ; then /usr/bin/sudo -u rjonasz -H -i /usr/local/bin/lockkde.sh else /bin/su rjonasz -c "DISPLAY=:0 xscreensaver-command --lock" fi fi
lockkde.sh
Konsole output #! /bin/sh
kde_pid=`pidof kdeinit5` export `cat /proc/$kde_pid/environ|grep -ao 'DBUS_SESSION_BUS_ADDRESS=[[:graph:]]*'` /usr/bin/qdbus org.freedesktop.ScreenSaver /ScreenSaver Lock
I can't seem to figure this one out.
Are you sure the PATH is set the same when run from a terminal versus when run by your key being removed? Try for debugging to do at the start of the script: echo $PATH > /tmp/$$.path.env Then after running it both ways you should have a file for each PID that did it that you can compare. -- Len Sorensen
participants (3)
-
Lennart Sorensen
-
Mauro Souza
-
Randy Jonasz