Skip to content

Instantly share code, notes, and snippets.

@dcoppari
Created July 12, 2019 13:25
Show Gist options
  • Save dcoppari/780594f8ae1df0218074ca6f5de0da01 to your computer and use it in GitHub Desktop.
Save dcoppari/780594f8ae1df0218074ca6f5de0da01 to your computer and use it in GitHub Desktop.
Bash script to monitor when computer is Idle and take information
#!/bin/bash
LOCKFILE=/tmp/notify.pid
HSTFILE=/tmp/history.tmp
if [ -e ${LOCKFILE} ] && kill -0 `cat ${LOCKFILE}`; then
echo "already running"
exit
fi
# make sure the lockfile is removed when we exit and then claim it
trap "rm -f ${LOCKFILE}; exit" INT TERM EXIT
echo $$ > ${LOCKFILE}
idle=false
idleAfter=300000 # consider idle after 5 minutes
while true; do
idleTimeMillis=$(xprintidle)
if [[ $idleTimeMillis -gt $idleAfter && $idle = false ]] ; then
timestart=`date -Iseconds`
opened=`wmctrl -l | tr "\n" '|'`
cp ~/.config/google-chrome/Default/History $HSTFILE
lastview=`sqlite3 $HSTFILE "select distinct(url) from urls order by last_visit_time desc limit 10;" | tr "\n" '|'`
idle=true
fi
if [[ $idleTimeMillis -lt $idleAfter && $idle = true ]] ; then
read -r -d '' JSONINFO << EOF
{
"hostname": "$(hostname)",
"timestart": "$timestart",
"idletime": "$idleTimeMillis",
"windows": "$opened",
"tabs": "$lastview"
}
EOF
echo $JSONINFO >> /tmp/lastreport.log
opened=""
idle=false
fi
sleep 1 # polling interval
done
rm -f ${LOCKFILE}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment