Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Desktop Environments
  • Search

kde4_shutdown.sh script for stopping KDE4 session, saving it

Problems with GUI applications? Questions about X, KDE, Gnome, Fluxbox, etc.? Come on in. NOTE: For multimedia, go up one forum
Post Reply
Advanced search
6 posts • Page 1 of 1
Author
Message
cz0
Guru
Guru
User avatar
Posts: 339
Joined: Mon Jun 13, 2005 8:50 am
Location: /earth/russia/moscow

kde4_shutdown.sh script for stopping KDE4 session, saving it

  • Quote

Post by cz0 » Fri Jun 18, 2010 8:56 am

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: Select all

# /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
Top
Grimi
n00b
n00b
Posts: 13
Joined: Tue Jul 08, 2003 9:02 am
Location: Germany

  • Quote

Post by Grimi » Thu Jul 26, 2012 4:25 pm

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
Top
SiliconBadger
n00b
n00b
Posts: 5
Joined: Thu Jun 28, 2012 1:19 am

Doesn't work for me

  • Quote

Post by SiliconBadger » Tue Dec 11, 2012 6:24 am

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.
kde-shutdown: line 18: unexpected EOF while looking for matching `''
kde-shutdown: line 34: syntax error: unexpected end of file
Top
urcindalo
l33t
l33t
Posts: 623
Joined: Tue Feb 08, 2005 2:58 pm
Location: Almeria, Spain

Re: Doesn't work for me

  • Quote

Post by urcindalo » Wed Jan 30, 2013 10:43 am

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.
kde-shutdown: line 18: unexpected EOF while looking for matching `''
kde-shutdown: line 34: syntax error: unexpected end of file
Same here :(
Top
Johanns
n00b
n00b
Posts: 1
Joined: Thu Jan 31, 2013 12:44 am

  • Quote

Post by Johanns » Thu Jan 31, 2013 1:10 am

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: Select all

 
# /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 
Top
steveL
Watchman
Watchman
Posts: 5153
Joined: Wed Sep 13, 2006 1:18 pm
Location: The Peanut Gallery

  • Quote

Post by steveL » Thu Jan 31, 2013 1:34 pm

You can replace:

Code: Select all

ps aux | grep 'startkde' | grep -v 'grep' | awk '{print $2}'
with:

Code: Select all

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: Select all

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: Select all

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: Select all

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: Select all

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
[topic=546828]update[/topic] - "a most excellent portage wrapper"

#friendly-coders -- We're still here for you™ ;)
Top
Post Reply

6 posts • Page 1 of 1

Return to “Desktop Environments”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy