Is there a programmable way to tell if the screen is locked?

I have lots of stuff in my crontab, including some that talks to my screen. If I'm not there for it to be notifying, I don't really want it to run.

Is there a way I can check to see if the computer is locked, so I can keep it from doing things when I'm not at my desk? Or, alternately, do other things only when I'm not at my desk?


Run this command:

gnome-screensaver-command -q | grep "is active"

if it has any output, the screen is locked.

In a shell script, you can use a clause like this:

if (gnome-screensaver-command -q | grep "is active");
then
    ...
fi

(my thanks to DoR, who originally posted these commands here)


light-locker

light-locker-command -q

or

light-locker-command -t


xscreensaver

You can watch the output of xscreensaver-command -watch or, if locking happens on blanking, use xscreensaver-command -time


See unity - how to detect if the screen is locked? for gnome-screensaver-command under Unity