'How to check if screen saver is running?

I have a script written in bash, which I need to take different route in case screensaver is currently running. This is Linux with Xorg and KDE. Script is called from cron, and it connects to DISPLAY=:0.

Any hint/idea how to check for state of screensaver?



Solution 1:[1]

If it is KDE4 you are running, you can use dbus to check if the screen saver is running:

$ qdbus org.freedesktop.ScreenSaver /ScreenSaver org.freedesktop.ScreenSaver.GetActive

or

$ qdbus org.kde.screensaver /ScreenSaver org.freedesktop.ScreenSaver.GetActive

This works also for gnome, but they are known for not following FreeDesktop standards, so you have to use:

$ qdbus org.gnome.ScreenSaver /ScreenSaver org.gnome.ScreenSaver.GetActive

Last but not least, you can check whether the standard X screen server is running using XScreenSaverQueryInfo() and checking the state field.

Solution 2:[2]

On Gnome 40 you can use

dbus-send --session --print-reply=literal --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.GetActive

It will print something like this

   boolean false

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 ultr
Solution 2 Awi