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 Answers 2

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)

6
  • 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, 2011 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. Apr 15, 2011 at 17:41
  • On second thought, it seems to be starting to get together. Thank you. Apr 15, 2011 at 18:24
  • 2
    You don't say "Thank you", but upvote, and mark it as 'accepted answer', when you're nice, please. :) Apr 15, 2011 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 Apr 15, 2011 at 20:23
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .