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.