'Lock all active KDE/Plasma sessions using CLI

I'm using a sophisticated algorithm depending on ACPI events on my laptop. Therefor I'm forced to lock my KDE/Plasma sessions from CLI. Previously I used this bash code to lock sessions:

for SESSION in $(qdbus --literal --system org.freedesktop.ConsoleKit /org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.GetSessions | sed 's/^.*\(Session[0-9]*\).*$/\1/'); do
    dbus-send --system --print-reply --dest=org.freedesktop.ConsoleKit /org/freedesktop/ConsoleKit/$SESSION org.freedesktop.ConsoleKit.Session.Lock
done

Commands are executed by acpid (by root) and not related to any actual user session. But some tome ago ConsoleKit was deprecated and not available via DBus anymore.

I found that:

dbus-send --session --dest=org.freedesktop.ScreenSaver --type=method_call /org/freedesktop/ScreenSaver org.freedesktop.ScreenSaver.Lock

executed from an actual user will lock the active session, but will not work if executed from root.

Any ideas hot to make it work if executed by acpid to lock all active KDE/Plasma sessions?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source