The following guide is for Debian/Ubuntu, you should adapt the code if you are using an other distribution or a custom Motion/Xscreensaver install


  • Motion (from repositories) (to capture from webcam while the screen is locked)

  • Xscreensaver (from repositories) (not gnome-screensaver, the original xscreensaver)

  • build-essentials (from repositories) (to compile the watchdog)

Install the requirements according to your distribution (Debian way : sudo apt-get install build-essentials motion xscreensaver).

How it works ?

Xscreensaver has a very handy utility : xscreensaver-command. You can lock and unlock the screen thanks to this tool, but you can also “watch” for status changes !

All we have to do is to write a daemon that will fork to start xscreesaver-command, and wait for its stdout result from a pipe. If it locks, we will fork to start Motion, and kill that child as soon as the screen is unlocked.

The environment

Create a directory named “motion” in /home to store the captured pictures from webcam.

All-in-one command line : mkdir /home/motion

The daemon

Compile the attached code (at the end of this article), and place the result in /usr/local/bin/xscreensaver_watchdog, and make it executable.

All-in-one command line : gcc -O3 xscreensaver_watchdog.c -o xscreensaver_watchdog && mv xcreensaver_watchdog /usr/local/bin && chmod +x /usr/local/bin/xscreensaver_watchdog

The init script

As our daemon needs a graphic TTY to run (xscreensaver-command is tty-dependant), we can’t create a startup script for this daemon.

You have to set it at the session startup, every decent Destkop environment (KDE, Gnome, Xfce, LXDE) has a way to do it.


Well, I had a lot of fun writing this tiny program, and it is giving me new ideas.

Why not recording the microphone input while the screensaver is locked ? It may be useful to know what your colleagues are saying when your are way.

I won’t write that program, but you still can. ;)