Way to measure computer usage via screensaver active/not active time?

Solution 1:

I would like to thanks and contribute back a simple, raw script (can be improved), put it on startup applications with:

xterm -e logSessionLock.sh

it is system crash/blackout prepared too. It is not heavly tested... but is working great til now. it will create 2 files, one at $HOME (the log) another at /tmp (the sys crash workaround)

logSessionLock.sh

#!/bin/bash

logFile="."`basename $0`".log"

function FUNClog {
  strTime=`gnome-screensaver-command --time`
  strDate=`date +"%Y%m%d-%H%M%S"`
  strLog="$strDate ($1) $strTime"
}

function FUNCwriteLog {
  echo $strLog >>$HOME/$logFile
  echo $strLog
}

active=false
firstTime=true

# restores last log entry in case of a system crash
strLog=`cat "/tmp/$logFile.tmp"`
FUNCwriteLog
echo "" >"/tmp/$logFile.tmp"
if [[ -n "$strLog" ]]; then #if has something, it was active when the system crashed
  active=true
fi

while true; do 
  if gnome-screensaver-command --query |grep -q "The screensaver is active"; then
    if ! $active; then
      # restore the saved tmp log
      strLog=`cat "/tmp/$logFile.tmp"`
      FUNCwriteLog

      # update log (can be off when this line is reached? once it seem to happen...)
      FUNClog BEGIN
      FUNCwriteLog #logs the begin time

      active=true
    else
      FUNClog ON #stores the ammount of time the screensaver has been ON
      echo $strLog >"/tmp/$logFile.tmp"
    fi
  else
    if $active; then
      FUNCwriteLog #saves the ammount of time the screensaver has been ON

      FUNClog OFF
      FUNCwriteLog
      echo "" >"/tmp/$logFile.tmp"

      active=false
    fi
  fi 

  sleep 1
done

the log is like this:

 20120214-191528 (BEGIN) The screensaver has been active for 0 seconds.
 20120214-193602 (ON) The screensaver has been active for 1234 seconds.
 20120214-193603 (OFF) The screensaver is not currently active.

Solution 2:

The "Workrave" package not only tracks whether you're using your computer and helps you take breaks during the day, but also provides a nice set of statistics, both raw (in a text file) and via a GUI (Daily usage: 5:41:00 for Jul 21). The stats also include stuff like minutes of mouse usage, mouse movement distance, keystrokes etc.

Install it from the official repositories, add it to your menu bar, right click and choose "statistics". You'll get a calendar to choose the day you want to know about. Or look at the data in ~/.workrave/historystats

Solution 3:

If you remove the --profile you'll get a format missing timestamps, but it does have whether or not the screensaver is active.

$ dbus-monitor "type='signal',interface='org.gnome.ScreenSaver'
...
signal sender=:1.46 -> dest=(null destination) serial=1881 path=/org/gnome/ScreenSaver; interface=org.gnome.ScreenSaver; member=ActiveChanged
boolean true

I've used a modification of this PHP script to activate or deactivate things based on my screensaver

#!/usr/bin/php
<?php
$handle = popen("dbus-monitor 'path=/org/gnome/ScreenSaver, member=ActiveChanged' 2>&1", 'r');
echo "'$handle'; " . gettype($handle) . "\n";
while (!feof($handle)) {
    $read = fgets($handle);
    if(preg_match("/^\s+boolean (\w+)/", $read, $matches))
    {
                $active = ($matches[1] == 'true');
                // do something here
    }
}
pclose($handle);
?>

The other option is to use gnome-screensaver-command --query. Using crontab I let bitcoin use all 4 cores when the screensaver is active, but it only gets 1 core when I'm using my computer.

DISPLAY=":0.0"
* * * * * if gnome-screensaver-command --query 2>&1 | grep -q 'is active'; then bitcoind setgenerate true 4; else bitcoind setgenerate true 1; fi

DISPLAY: Without setting DISPLAY gnome-screensaver-command won't be able to find the screen when run from cron. This must run as the same user logged in.

2>&1: This directs any errors into standard output, which is captured by ...

| grep -q 'is active';: the -q makes the grep quiet, it doesn't output anything. But the command returns a success or failure that is used by if.

I realize none of these are a complete solution, but hopefully they're enough to get you started.

Solution 4:

This is a more complete script. You can trigger it from cron every minute and if the screensaver is active, it will record how long it's been active. Once it deactivates it'll take the last measurement and add it to ~/Screensaver.log. If you run it from cron it could be inaccurate for up to 59 seconds each time the screensaver deactivates.

#!/bin/bash
export DISPLAY=':0'
if gnome-screensaver-command -q 2>&1 | grep -q 'is active'; then
    # this will overwrite the file each time.
    gnome-screensaver-command --time > /tmp/ScreensaverActiveTime
# if the screensaver is not active and we have the file from last time this ran ...
elif [ -e /tmp/ScreensaverActiveTime ]; then
    # append it to our log and ...
    cat /tmp/ScreensaverActiveTime >> ~/Screensaver.log
    # remove the file. It will be recreated once the screensaver activates again.
    rm /tmp/ScreensaverActiveTime
fi