Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 7 down vote accepted

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");

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

share|improve this answer
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
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
But I haven't implemented it enough to accept it as an answer. – Dave Jacoby Apr 15 '11 at 19:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.