10

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?

2 Answers2

12

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)

  • 4
    When you use grep in a test, its best to use grep -q so that there is no output to the console. Also the parenthesis are redundant - they cause a sub-shell to be created though there's no need for that in this case. – Guss Apr 15 '11 at 17:39
  • Testing it now to see if I can build on it. It seems to be somewhat fragile and inconsistent in first testing, but that might be just me. – Dave Jacoby Apr 15 '11 at 17:41
  • On second thought, it seems to be starting to get together. Thank you. – Dave Jacoby Apr 15 '11 at 18:24
  • 2
    You don't say "Thank you", but upvote, and mark it as 'accepted answer', when you're nice, please. :) – user unknown Apr 15 '11 at 19:29
  • 1
    @VarLogRant no rush or anything, just to let you know, you can always change your mind about the accepted answer by just clicking the green check mark again to undo it – Stefano Palazzo Apr 15 '11 at 20:23
  • nowadays 2014, it only shows "is active" if the screen got blanked :(, I believe unity is actually doing the locking? – Aquarius Power Aug 02 '14 at 03:32
1

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

unhammer
  • 2,271