This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Lock all sessions with bash script?

Tags: None
(comma "," separated)
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
You may need to use "dbus-send" in that case.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
KarlisRepsons
Registered Member
Posts
42
Karma
0
If I replace with
dbus-send --dest='org.kde.screensaver' /ScreenSaver Lock
, it still doesn't work and complains about X11 initialization... Any suggestions?


KarlisRepsons, proud to be a member of KDE forums since 2008-Nov.
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
Try sourcing ~/.dbus/session-bus/* prior to running that command.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
KarlisRepsons
Registered Member
Posts
42
Karma
0
So this script:
Code: Select all
#!/bin/bash

get_login_users() {
  who -u | egrep -o "^[^ ]*" | sort | uniq
}

do_lock() {
  for u in $(get_login_users)
  do
    . /home/$u/.dbus/session-bus/*
    sudo -u $u dbus-send --dest='org.kde.screensaver' /ScreenSaver Lock
  done
}

do_lock


outputs this:
Failed to open connection to "session" message bus: /usr/bin/dbus-launch terminated abnormally with the following error: Autolaunch error: X11 initialization failed.

/opt/scripts-system/lock_sessions: line 10: /home/root/.dbus/session-bus/*: No such file or directory
Failed to open connection to "session" message bus: /usr/bin/dbus-launch terminated abnormally with the following error: Autolaunch error: X11 initialization failed.



And with this the result is similar:
Code: Select all
do_lock() {
  for u in $(get_login_users)
  do
    if [[ $u != root ]]
    then
        sudo -u $u /bin/bash -c ". /home/$u/.dbus/session-bus/* && dbus-send --dest='org.kde.screensaver' /ScreenSaver Lock"
    fi
  done
}


Next ideas?


KarlisRepsons, proud to be a member of KDE forums since 2008-Nov.
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
Can you post the full output of the second one? Did it have just an X error? Or more than that?

You may also need to set the DISPLAY variable. Not sure how you are going to detect that however.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
KarlisRepsons
Registered Member
Posts
42
Karma
0
Well, the output is identical except for root warning (that if statement precludes it)...


KarlisRepsons, proud to be a member of KDE forums since 2008-Nov.
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
My testing indicates that you need to set DISPLAY as well. You may be able to use the output of 'ck-list-sessions' or 'who' to find out what is needed.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Sogou [Bot]