Skip to content

Instantly share code, notes, and snippets.

@talatham
Last active October 24, 2018 11:23
Show Gist options
  • Save talatham/5bc336c4a93700404850e315e136ad4c to your computer and use it in GitHub Desktop.
Save talatham/5bc336c4a93700404850e315e136ad4c to your computer and use it in GitHub Desktop.
Monitor mouse movement. If mouse hasn't moved in a set period of time within certain window, shut down the box.
#!/bin/bash
# ---------------------------------------------------------------------------#
# Mousewatcher v1.2
# Monitor user activity and shutdown machine if no activity detected.
#
# 24/05/2010 - Tom Latham
#
# Explanation:
# * Log output of /dev/input/mice to file. Monitor timestamp of file to
# determine user activity. If file is not modifed for a specified
# period of time whilst outside of core hours, shutdown machine.
# * Supercedes Mousewatcher v0.5. Interrupt handling proved to be unusable
# for devices connected via USB due to shared handlers for USB devices.
# * No keyboard monitoring required.
#
# Changelog:
# v1.0 - 22/04 (TAL) - Test logging of mouse activity.
# v1.1 - 27/04 (TAL) - Added: out of hours window and shutdown event.
# v1.2 - 24/05 (TAL) - Added: movement threshold to ignore false positives.
# ---------------------------------------------------------------------------#
WAIT=30m #Set period between checks
LIMIT=4 #Number of checks to wait before shutdown. Example: Inactivity for 4x30m = 2hr, causes shutdown.
LOGFILE=/var/log/mousewatcher.log #Set log file for program execution
MOUSEFILE=/var/log/mousemovement.log #Set log file for mouse movement
UPPER=18:00:00 #Upper boundary of time window
LOWER=06:00:00 #Lower boundary of time window
THRESHOLD=200 #Movement threshold
echo "`date` - Test period set to $WAIT. Monitoring activity..." > $LOGFILE #Create log file
monitor_mouse()
{
# -------------------------------------------------------------------------#
# Create log file of mouse movement. Return start time of mouse logging.
# -------------------------------------------------------------------------#
rm $MOUSEFILE; cat /dev/input/mice > $MOUSEFILE &
local start=$(date +%s); echo "$start"
}
get_time()
{
#--------------------------------------------------------------------------#
# Return current time in readable format. For use in execution logging.
#--------------------------------------------------------------------------#
local now=$(date +"%d-%m-%Y %T"); echo "$now"
}
get_lastmovement()
{
#--------------------------------------------------------------------------#
# Return last modified date of mouse log. This equates to last movement.
#--------------------------------------------------------------------------#
local last=$(date -r $MOUSEFILE +%s); echo "$last"
}
get_withinwindow()
{
#--------------------------------------------------------------------------#
# Compare current time with upper and lower boundary.
# Return result of comparion (0=true, within time frame).
#--------------------------------------------------------------------------#
local upper=$(date -d $UPPER +%s)
local lower=$(date -d $LOWER +%s)
local now=$(date +%s)
[[ now -gt lower && now -le upper ]]; echo $?
}
run_mousewatcher()
{
#--------------------------------------------------------------------------#
# Monitor mouse movement. If mouse not moved within specified time period
# (check period * limit) and time is outside of window, shut down box.
#--------------------------------------------------------------------------#
COUNT=0; sleep 1 #Wait one minute before starting monitoring to allow for changes during boot cycle.
START=$(monitor_mouse)
echo "$(get_time) - Monitoring started at: `date --date=@$START +"%d-%m-%Y %T"`." >> $LOGFILE
while true #Repeat until exit
do
sleep $WAIT
LAST=$(get_lastmovement)
MOVEMENT=`stat -c %s $MOUSEFILE`
echo "$(get_time) - Last movement at: `date --date=@$LAST +"%d-%m-%Y %T"`. Movement size: $MOVEMENT." >> $LOGFILE
#If last modified time is not equal to start time then mouse activity detected. Reset monitoring.
if [[ $LAST -ne $START && $MOVEMENT -gt 200 ]] ; then
COUNT=0; START=$(monitor_mouse)
echo "$(get_time) - Activity detected. Monitoring reset at: `date --date=@$START +"%d-%m-%Y %T"`." >> $LOGFILE
else
let COUNT+=1
if [[ $COUNT -gt $LIMIT ]] ; then
WINDOW=$(get_withinwindow)
if [[ $WINDOW == 1 ]] ; then
echo "$(get_time) - No movement detected. Maximum time reached. Shutting down..." >> $LOGFILE
shutdown -h now #Shutdown machine
else
echo "$(get_time) - No movement detected. Maximum time reached. During business hours, don't shut down." >> $LOGFILE
fi
else
echo "$(get_time) - No movement detected. Check: $COUNT." >> $LOGFILE
fi
fi
echo >> $LOGFILE
done
}
run_mousewatcher & #Run mousewatcher as background process
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment