Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
kde4_shutdown.sh script for stopping KDE4 session, saving it
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Desktop Environments
View previous topic :: View next topic  
Author Message
cz0
Apprentice
Apprentice


Joined: 13 Jun 2005
Posts: 181
Location: /earth/russia/moscow

PostPosted: Fri Jun 18, 2010 8:56 am    Post subject: kde4_shutdown.sh script for stopping KDE4 session, saving it Reply with quote

Here comes my script, that can shutdown KDE 4 saving current session. It can do the work from a regular user (session owner) and root, that makes it suitable for been run from acpid. Maybe it will help somebody. All comments are welcome.

Code:

# /bin/sh
# Written by cz0 2010
# Distributed under the terms of the GNU General Public License v2

SU="/bin/su"
QDBUS="/usr/bin/qdbus"
TIMEOUT="20"
USER="$(/usr/bin/whoami)"

for KDEPID in $(ps aux | grep 'startkde' | grep -v 'grep' | awk '{print $2}'); do
        KDEUSER=$(ps u $KDEPID | grep 'startkde' | awk '{print $1}')

        if [[ "$KDEUSER" != "$USER" && "$USER" != "root" ]] ; then
                logger "kde4_shutdown: unable to stop KDE session of user $KDEUSER"
                exit 67
        fi

        CURRENT_DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(ps h --ppid $KDEPID -o pid | awk '{print $1}')/environ | sed -e 's/DBUS_SE>
        export DBUS_SESSION_BUS_ADDRESS=$CURRENT_DBUS_SESSION_BUS_ADDRESS

        logger  "kde4_shutdown: sending shutdown signal to KDE session pid=$KDEPID owner uid=$KDEUSER via $DBUS_SESSION_BUS_ADDRESS"

        if [[ "$USER" == "root" ]] ; then
                $SU -c "$QDBUS org.kde.ksmserver /KSMServer logout 0 0 0" $KDEUSER
        else
                $QDBUS org.kde.ksmserver /KSMServer logout 0 0 0
        fi

        while [[ "$(ps h --ppid $KDEPID -o pid)" != "" && $TIMEOUT > 0 ]] ; do
                sleep 1s
                let "TIMEOUT -= 1"
        done
done
Back to top
View user's profile Send private message
Grimi
n00b
n00b


Joined: 08 Jul 2003
Posts: 13
Location: Germany

PostPosted: Thu Jul 26, 2012 4:25 pm    Post subject: Reply with quote

I wanted to logoff without killing the X-Server because out of the blue my kde froze.

This qdus thingy works pretty well. I stumbled upon this via some other forums and traced it back to here. And because i myself use gentoo, so I just want say thank you for sharing this information! :D
Back to top
View user's profile Send private message
SiliconBadger
n00b
n00b


Joined: 28 Jun 2012
Posts: 4

PostPosted: Tue Dec 11, 2012 6:24 am    Post subject: Doesn't work for me Reply with quote

Copied and pasted into a text file, made it executable, and I get this error when running the script. Is it supposed to take any arguments? I don't know anything about scripts.

Quote:

kde-shutdown: line 18: unexpected EOF while looking for matching `''
kde-shutdown: line 34: syntax error: unexpected end of file
Back to top
View user's profile Send private message
urcindalo
Guru
Guru


Joined: 08 Feb 2005
Posts: 553
Location: Almeria, Spain

PostPosted: Wed Jan 30, 2013 10:43 am    Post subject: Re: Doesn't work for me Reply with quote

SiliconBadger wrote:
Copied and pasted into a text file, made it executable, and I get this error when running the script. Is it supposed to take any arguments? I don't know anything about scripts.

Quote:

kde-shutdown: line 18: unexpected EOF while looking for matching `''
kde-shutdown: line 34: syntax error: unexpected end of file


Same here :(
Back to top
View user's profile Send private message
Johanns
n00b
n00b


Joined: 31 Jan 2013
Posts: 1

PostPosted: Thu Jan 31, 2013 1:10 am    Post subject: Reply with quote

I figured out why the posted script was giving errors. The line that begins with "CURRENT_DBUS_SESSION_BUS_ADDRESS=" and ends with ">" seems to have been truncated. It seems like there is (or at least was) a KDE issue where an important environment variable DBUS_SESSION_BUS_ADDRESS is/was not set properly. This part of the script appears to be a workaround for this problem. I changed the script to automatically set this variable correctly, but only if it is not already set. I think this is safer since at least on my system this variable is already set. I did test that this clever technique for figuring out how KDE set this variable for itself actually works fine.

Below is my attempt at a corrected version. It works for me under Suse 12.1.

I am not sure why Kde does not come with a shell script like this one to allow logging out of Kde without resorting to the menu.

Code:
 
# /bin/sh
# Written by cz0 2010
# Distributed under the terms of the GNU General Public License v2

SU="/bin/su"
QDBUS="/usr/bin/qdbus"
TIMEOUT="20"
USER="$(/usr/bin/whoami)"

for KDEPID in $(ps aux | grep 'startkde' | grep -v 'grep' | awk '{print $2}'); do
        KDEUSER=$(ps u $KDEPID | grep 'startkde' | awk '{print $1}')

        if [[ "$KDEUSER" != "$USER" && "$USER" != "root" ]] ; then
                logger "kde4_shutdown: unable to stop KDE session of user $KDEUSER"
                exit 67
        fi

        # If the DBUS_SESSION_BUS_ADDRESS environment variable is not already set correctly
        # then set it by finding the environment file for the startkde process in proc and
        # parsing it to get get the correct setting.
        if [ -z "$DBUS_SESSION_BUS_ADDRESS" ]; then
                ENVIRON_FILE=/proc/$(ps h --ppid $KDEPID -o pid | awk '{print $1}')/environ
                CURRENT_DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS $ENVIRON_FILE | sed -e 's/DBUS_SESSION_BUS_ADDRESS=//')
                export DBUS_SESSION_BUS_ADDRESS=$CURRENT_DBUS_SESSION_BUS_ADDRESS
        fi

        logger  "kde4_shutdown: sending shutdown signal to KDE session pid=$KDEPID owner uid=$KDEUSER via $DBUS_SESSION_BUS_ADDRESS"

        if [[ "$USER" == "root" ]] ; then
                $SU -c "$QDBUS org.kde.ksmserver /KSMServer logout 0 0 0" $KDEUSER
        else
                $QDBUS org.kde.ksmserver /KSMServer logout 0 0 0
        fi

        while [[ "$(ps h --ppid $KDEPID -o pid)" != "" && $TIMEOUT > 0 ]] ; do
                sleep 1s
                let "TIMEOUT -= 1"
        done
done
Back to top
View user's profile Send private message
steveL
Advocate
Advocate


Joined: 13 Sep 2006
Posts: 2519
Location: The Peanut Gallery

PostPosted: Thu Jan 31, 2013 1:34 pm    Post subject: Reply with quote

You can replace:
Code:
ps aux | grep 'startkde' | grep -v 'grep' | awk '{print $2}'

with:
Code:
ps aux | awk '/startkde/ && ! /awk/ { print $2 }'

which will save two forks of grep and make things a bit quicker. pgrep from sys-process/procps is a much better method, however.
You can even speed it up further without using regexen at all:
Code:
ps aux | awk 'index ($0, "startkde") && ! index($0, "awk") { print $2 }'

Actually I see you're getting the user id as well: I'd do this all in one with:
Code:
ps aux | awk -v u="$LOGNAME" '/startkde/ && ! /awk/ && $1 == u { print $2 }'

Not sure if that's right for your logic checking against username, though. Just: { print $1, $2 } gets the info, but you'd have to do:
Code:
local u pid data
data=$(ps aux | awk '/startkde/ && ! /awk/ { print $1, $2 }'
while read -r u pid; do
case $u in
   "$LOGNAME") :
;;   *) logger "kde4_shutdown: unable to stop KDE session of user $u"
   exit 67
esac
done << EOF
$data
EOF

or the like to process the data, since we don't have BASH < <(process substitution) nor <<< "$var" input.

Just for ref, with pgrep that'd be:
Code:
pgrep -u "$LOGNAME" startkde

LOGNAME is the standard POSIX environment variable for the currently logged-in user.

HTH,
steveL
_________________
creaker wrote:
systemd. It is a really ass pain

update - "a most excellent portage wrapper"

#friendly-coders -- We're still here for you™ ;)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Desktop Environments All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum