4

I have a cron which runs a script every hour, it uses a notify-send for a notification on the Ubuntu 18.04 desktop (Gnome) once it starts.

What can I add to the script to:

  1. If screen is locked then exit.
  2. If screen is not locked then continue as normal with notification.

The answer below lead me to depending on environment:

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

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

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

2 Answers2

5

There is a setting to hide notifications while the lock screen is enabled:

GUI settings

You can also modify this setting via the command line:

gsettings set org.gnome.desktop.notifications show-in-lock-screen false

But, since you asked, you can also use gdbus on the command line with the --session parameter and org.gnome.ScreenSaver to determine if the screen is locked.

0

Test if screen saver is active

qdbus will tell you if the screen saver (used by Lock Screen) is active. First you need to know which environment you are using: Gnome, Unity, KDE, etc.

In this example, Unity is active whilst Gnome and KDE are not:

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

$ qdbus org.gnome.ScreenSaver /ScreenSaver org.gnome.ScreenSaver.GetActive Error: org.freedesktop.DBus.Error.UnknownMethod No such interface 'org.gnome.ScreenSaver' on object at path /ScreenSaver

$ qdbus org.kde.screensaver /ScreenSaver org.freedesktop.ScreenSaver.GetActive Service 'org.kde.screensaver' does not exist.

For the Gnome desktop the second option will return false and an error message will appear for the first and third options.