通过屏幕保护程序活动/非活动时间来测量计算机使用情况的方法?

我正在寻找一种简单的方法来确定我每天在计算机上花费的大量时间。 如果你试图监控进程,按键,鼠标点击等,这可能是一项艰巨的任务,因为人们可以通过思考编码问题,阅读网页文章,通过电话交谈或走走狗。 电脑无法读懂我的想法。 由于我将计算机全天24小时监控,因此登录无法正常工作。

我想到了记录计算机在屏幕保护模式下花费了多少时间的想法。 我的错误将不会超过屏幕保护程序的空闲时间和屏幕保护程序模式的次数。 从24小时内减去这个数字会给我一个估计,这对我来说是合理的。

问题是:当屏幕保护程序打开和关闭时,我不知道如何记录。 我现在正在大多数机器上运行Ubuntu 10.10,即将开始升级到11.04。

有任何想法吗?

[编辑]经过更多的谷歌搜索后,我点击了看起来可能有用的dbus-monitor,但却缺少一个重要的成分。 这是我正在运行的脚本,它将监视器作为守护进程启动:

#!/bin/bash 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 & fi 

以下是锁定和解锁屏幕几次后产生的输出:

 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 

第二列显然是unix UTC,只需几秒钟。 缺少的成分是它无法识别屏幕保护程序是打开还是关闭! 我想我可以假设他们从NameAcquired发生时开始切换,但是让我感到不安的是,可能会有一个丢失的或额外的事件,我无法预料哪些会导致一切不同步。

很有必要提出想法。

jkcunningham

我想感谢并回馈一个简单的原始脚本(可以改进),将它放在启动应用程序上:

 xterm -e logSessionLock.sh 

这也是系统崩溃/停电准备。 它没有经过严格测试……但是现在工作得很好。 它将创建2个文件,一个在$ HOME(日志),另一个在/ tmp(sys崩溃解决方法)

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 

日志是这样的:

  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. 

“ Workrave ”软件包不仅可以跟踪您是否正在使用计算机并帮助您在白天rest,还可以提供一组很好的统计信息,包括原始(在文本文件中)和GUI( Daily usage: 5:41:00 for Jul 21 )。 统计数据还包括鼠标使用时间,鼠标移动距离,按键等。

从官方存储库安装它,将其添加到菜单栏,右键单击并选择“统计信息”。 你会得到一个日历来选择你想知道的日子。 或者查看〜/ .workrave / historystats中的数据

如果删除–profile,您将获得缺少时间戳的格式,但它确实具有屏幕保护程序是否处于活动状态。

 $ 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 

我已经使用这个PHP脚本的修改来激活或停用基于我的屏幕保护程序的东西

 #!/usr/bin/php &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); ?> 

另一种选择是使用gnome-screensaver-command --query 。 使用crontab我让比特币在屏幕保护程序处于活动状态时使用所有4个核心,但在我使用计算机时它只获得1个核心。

 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 gnome-screensaver-command从cron运行时将无法找到屏幕。 这必须在同一用户登录时运行。

2>&1 :这会将任何错误导入标准输出,由…捕获

| grep -q 'is active'; :-q使grep安静,它不输出任何东西。 但该命令返回if使用的成功或失败。

我发现这些都不是一个完整的解决方案,但希望它们足以让你开始。

这是一个更完整的脚本。 您可以每分钟从cron触发它,如果屏幕保护程序处于活动状态,它将记录它的活动时间。 一旦它停用,它将进行最后一次测量并将其添加到〜/ Screensaver.log。 如果从cron运行它,每次屏幕保护程序停用时,最多可能会有59秒的不准确。

 #!/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 

我使用uptime命令,但它没有给出系统的活动/非活动时间。 正常运行时间显示以下信息的一行显示。 当前时间,系统运行的时间,当前登录的用户数以及过去1,5和15分钟的系统负载平均值。