#!/bin/bash # Copyright (c) 2009 Jonathan Kamens . # # May be distributed under the terms of the Gnu General Public # License, Version 3 or any newer version at your discretion. See # http://www.gnu.org/licenses/gpl.html for license terms. # Script for stopping / starting the "motion" service based on whether # you're actually sitting in front of the computer. The point is to # keep an eye of who's messing with your desk / computer when you're # not around, while not filling up your disk with motion captures from # when you're there. # # The script is designed to work with GNOME (assumes gnome-screensaver # is running and and uses zenity to prompt the user) but can probably # be modified relatively easily to work with other desktops. # # The script ensures that the service is always running when the # screen is locked, and prompts you whether to suspend it when the # screen is unlocked. By default it re-prompts every fifteen minutes, # which can be adjusted by changing the setting of the prompt_timeout # variable below. The reason it prompts rather than automatically # disabling the service when the screen is unlocked is because you # might have unlocked the screen remotely (e.g., with VNC). # To use the script, you need to: # # 1. Save the script somewhere. # 2. Make it executable. # 3. Create a "tmp" subdirectory of your home directory for the log # file to go, or change the log file path below. # 4. Modify /etc/sudoers so that requiretty is not set and so that # your userid has permission to stop and start the motion service # without a password (for example, "jik ALL=(root) NOPASSWD: # /sbin/service motion stop, /sbin/service motion start"). # 5. Add the script to your startup applications (System > Preferences # > Startup Applications). # 6. Log out and log back in to start it up. # 7. Check the log file for errors if it doesn't behave as expected. exec &>$HOME/tmp/motion-monitor.log echo motion-monitor.sh PID $$ starting # on startup: # # running locked was_locked expired action # ------- ------ ---------- ------- ------ # n n n/a n/a prompt # # after startup: # # running locked was_locked expired action # ------- ------ ---------- ------- ------ # y n n/a y prompt # y n y n/a prompt # n y n/a n/a start prompt_timeout=$((15*60)) check_interval=5 mailed=n get_state_variables() { local status if service motion status &>/dev/null; then running=y else running=n fi if [ "$locked" = "y" ]; then was_locked=y else was_locked=n fi status=$(gnome-screensaver-command -q 2>&1) case "$status" in *"Screensaver is not running"*|*"The screensaver is inactive"*) locked=n; mailed=n ;; *"Failed to connect to the D-BUS daemon"*) echo "DBUS connection failed; watch-monitor.sh PID $$ exiting" exit ;; *"The screensaver is active"*) locked=y; mailed=n ;; *) if [ "$mailed" = "n" ]; then echo "Unrecognized output from gnome-screensaver-command -q: $status" | Mail -s "motion-monitor.sh error" $USER fi mailed=y ;; esac if [ -n "$prompted" ]; then if (($(date +%s)-prompted>$prompt_timeout)); then expired=y else expired=n fi else expired=y fi } stop() { sudo service motion stop } start() { sudo service motion start } prompt() { local text cmd if [ $running = y ]; then text='Suspend motion?' cmd='stop' else text='Start motion?' cmd='start' fi if zenity --question --text="$text" --timeout=10; then $cmd fi prompted=$(date +%s) } get_state_variables if [ $running$locked = nn ]; then prompt fi while :; do get_state_variables if [ $running$locked$expired = "yny" ]; then prompt elif [ $running$locked$was_locked = "yny" ]; then prompt elif [ $running$locked = "ny" ]; then start fi sleep $check_interval done # $Id: motion-monitor.sh,v 1.6 2009/12/24 23:04:17 jik Exp $