Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm looking for a simple way to determine the rough amount of time per day I spend on a computer. This can be a difficult task if you try to monitor processes, key presses, mouse clicks and the like, because one can be doing anything from thinking about a coding problem, reading a web article, talking on the phone, or gone off walking the dog. The computer cannot read my mind. Since I leave computers on 24/7 monitoring log-ins won't work.

I hit on the idea of logging how much time the computer spends in screensaver mode. My error would then be no greater than the product of the idle-time-to-screensaver with the number of times it goes into screensaver mode. Subtracting this from 24 hours would give me an estimate which would be reasonable for my purposes.

The problem is: I don't know how to log when the screensaver turns on and off. I am running Ubuntu 10.10 at the moment on most machines, about to start upgrading to 11.04 on some of them.

Any ideas?

[edit] After more googling I hit upon the dbus-monitor which looked like it might work, but is missing an important ingredient. Here's the script I am running which launches the monitor as a daemon:

RUNNING=`ps -A | grep "dbus-monitor"`
if [ ! $RUNNING  ]; then
    echo "(Re)starting dbus-monitor daemon"
    nohup dbus-monitor "--profile" "type='signal',interface='org.gnome.ScreenSaver'" >> ${HOME}/sc.log &

Here is the output it produces after locking and unlocking the screen a couple times:

sig     1304860712      153829  2       /org/freedesktop/DBus   org.freedesktop.DBus    NameAcquired
sig     1304860717      318732  462     /org/gnome/ScreenSaver  org.gnome.ScreenSaver   ActiveChanged
sig     1304860725      547928  463     /org/gnome/ScreenSaver  org.gnome.ScreenSaver   ActiveChanged
sig     1304861018      17      464     /org/gnome/ScreenSaver  org.gnome.ScreenSaver   ActiveChanged
sig     1304862919      403523  466     /org/gnome/ScreenSaver  org.gnome.ScreenSaver   ActiveChanged

The second column is obviously unix UTC in seconds. The missing ingredient is it doesn't identify whether the screensaver is on or off! I suppose I could assume they toggle from the time NameAcquired happens, but it makes me queasy that there might be a missing or extra event I can't anticipate which would throw everything out of sync.

Much obliged for ideas.


share|improve this question
Are time management tools like Hamster an option? –  Nathan Osman Jun 15 '11 at 6:21
I just installed it. It isn't clear how it will handle time I don't specifically log to tasks yet, but it does look interesting. –  jkcunningham Jun 16 '11 at 15:07
I've had it on now for half an hour or so and it looks like it requires that it be started each time. That's a drawback. I'm not trying to clock particular time for, say, charging purposes, just time spent at the computer. And if I forget to turn it on that time will be unaccounted for. I'm still playing with it though. –  jkcunningham Jun 16 '11 at 16:29
This is a very good question in regards to measuring time spend on the PC. Very nice idea about the screensaver. –  Luis Alvarado Feb 14 '12 at 21:52
Check this out on github github.com/nitesh-/hoursboard . This might answer your question –  Nitesh morajkar Sep 29 '13 at 7:08

5 Answers 5

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)



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


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

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"`

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

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

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


  sleep 1

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.
share|improve this answer

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

share|improve this answer
This looks like a very workable solution. I'd like to find a way to disable the "microbreak" reminders which can be rather annoying, but otherwise it appears to accumulate just what I'm looking for. Thanks! –  jkcunningham Jul 26 '11 at 2:06
@jkcunningham It is easy to enable or disable the microbreaks, rest breaks, or daily limit from the right-click/Preferences dialog. Though, of course, it is good for your eyes, wrists, arms, back etc. to actually take regular breaks, and workrave makes it easy (even fun) to do that while also making it easy to postpone or skip the occasional break if you're right in the middle of something. –  nealmcb Jul 26 '11 at 16:49

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

$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

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.

* * * * * 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.

share|improve this answer

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.

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
share|improve this answer
I like this approach. There might be a corner case issue if a machine goes down while in screensaver mode leaving a hanging /tmp/screensaveractivetime to be logged. For my purposes this would get around it: #!/bin/bash ## 4 states: ## * working ## * not working, but screensaver not yet active ## * screensaver active ## * computer power failure or turned off ## I want to log the first two states export DISPLAY=':0' SS_LAG=10 if gnome-screensaver-command -q 2>&1 | grep -q 'is inactive'; then DSTR=date "+%Y %m %d %H %M echo "$SS_LAG $DSTR" >> ${HTDOCS}/data/work.log fi –  jkcunningham Jun 16 '11 at 15:35
Okay, that didn't workout well - 5 minute max for edits. This time I'll get the code markdown in there: [code] #!/bin/bash # 4 states: # * working # * idle, screensaver inactive # * screensaver active # * computer off # I want to log the first two and the screensaver delay setting export DISPLAY=':0' SS_LAG=10 if gnome-screensaver-command -q 2>&1 | grep -q 'is inactive'; then DSTR=date "+%Y %m %d %H %M echo "$SS_LAG $DSTR" >> ${HTDOCS}/data/work.log fi [/code] I give: how do you create a code block in a comment? 4 spaces don't work. –  jkcunningham Jun 16 '11 at 16:04

I use uptime command, but it does not give the system's active/inactive time. uptime gives a one line display of the following information. The current time, how long the system has been running, how many users are currently logged on, and the system load averages for the past 1, 5, and 15 minutes.

share|improve this answer
My current uptime is 81 days 23 hours 25 minutes. I assure you I haven't been sitting in front of a computer that long. Thanks, but that won't work. I'm looking for a way to determine - roughly - how long I sit in front of computers on a daily basis. That's why I'm looking at the screensaver active/inactive times. –  jkcunningham May 8 '11 at 15:48
That's good. Yeah, as I mentioned it won't give you the system's active/inactive time and I don't know a way to calculate it from screensaver active/inactive times. –  Chethan S. May 8 '11 at 15:51

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.