Home > ubuntu > Some notes on activating/deactivating the screensaver

Some notes on activating/deactivating the screensaver

I had to face the following scenario. I have a script running in the background. When a condition is fulfilled, the script opens a given webpage and takes a screenshot.


  1. If I leave the machine alone and the screensaver gets activated, I get a beautiful black screenshot :( So first, I had to switch the screensaver off (see this post).
  2. Some kind of screensaver would still be useful. I can’t leave the screen on 24/7 and I don’t want to switch the monitor on/off each time I enter/leave the room.

To solve the 2nd difficulty, I came up with the following solution. I wanted a screensaver that I can activate/deactivate from a script. Under deactivation I mean that the screensaver is on, and from a script I can poke/wake up the screensaver without touching the mouse or the keyboard.


sudo apt-get install gnome-screensaver

Activate it:

gnome-screensaver-command -a

Deactivate it (demo):

gnome-screensaver-command -a && sleep 3 && gnome-screensaver-command -d

In addition, since I switched off the screensaver completely (see difficulty #1), I wanted to be able to activate the screensaver with a keyboard shortcut (with Ctrl+Alt+S for instance when I leave the room). Here is how to do it:

  • Open the Keyboard utility from the Dash.
  • Under the tab Shortcuts, select Custom Shortcuts. With the “+” button add the command “gnome-screensaver-command -a“. Click on “Disabled” and press the new shortcut (I suggest Ctrl+Alt+S).
  • See this page for some screenshots.

In your script, do something like this for activating/deactivating the screensaver (pseudo code):

# The screensaver may be on or off. If it's on, I suppose 
# you activated it with Ctrl+Alt+S, as explained above.
if (some condition is met):
    external_call("gnome-screensaver-command -d")
    make a screenshot
    external_call("gnome-screensaver-command -a")
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: