All pastes #1904995 Raw Edit

idlelog.sh

public shellscript v1 · immutable
#1904995 ·published 2010-07-21 03:16 UTC
rendered paste body
#!/bin/bash# you can edit these:threshold=300 # how many seconds to consider idlelongidle=1200 # how many seconds to consider a long idleabsentidle=1800 # how many seconds to give up and decide you're absentbuttons="overslept,food,went out,thinking,watched something,bathroom,chores,\other" # list of standard reasons for being idlefunction alert_me() {  if [ -z "$(pgrep -f mplayer.*urandom)" ];then    pkill mplayer    sleep 2 && mplayer -really-quiet -ao alsa:device=lame -demuxer rawaudio \      -endpos 120 /dev/urandom  fi}# end editsgoneidle=0mkdir -p ~/.idlefunction getreason() {  reason="$(xmessage -print -nearmouse -buttons "$buttons"  "Why idle @ $1?")"  [ -f ~/.idle/log.tmp ] && [ -z $(pgrep xmessage) ] && { cat ~/.idle/log.tmp |\    sed -e "s/()/(unspecified)/" >~/.idle/log;rm ~/.idle/log.tmp; }  mv ~/.idle/log ~/.idle/log.tmp  cat ~/.idle/log.tmp | sed -e "s/\(went idle @ $1\) ()/\1 ($reason)/" \    >~/.idle/log  rm ~/.idle/log.tmp}while sleep 1;do  idleplus=$(xidles)  if [ $idleplus -ge $threshold ];then    if [ $goneidle -eq 0 ];then      goneidle=1      nowtime=$(date +%s)      idletime=$((nowtime-idleplus))      reason="$(cat ~/.idle/reason 2>/dev/null)"      echo "went idle @ $(date -d @$idletime) ($reason)" >>~/.idle/log    fi  [ $idleplus -ge $longidle ] && [ $idleplus -lt $absentidle ] && \    [ -z "$reason" ] && alert_me &  else    if [ $goneidle -gt 0 ];then      goneidle=0      nowtime=$(date +%s)      returntime=$((nowtime-idleplus))      gfs=$((returntime-idletime))      gonefor="$((gfs/3600/24))d $((gfs/3600%24))h $((gfs/60%60))m $((gfs%60))s"      echo "came back @ $(date -d @$returntime) ($gonefor)" >>~/.idle/log      >~/.idle/reason      [ -z "$reason" ] && getreason "$(date -d @$idletime)" &    fi  fidone