[Tfug] Conditional excution of a script based on system usage

Claude Rubinson rubinson at u.arizona.edu
Mon Jun 6 18:34:33 MST 2005


On Tue, Apr 12, 2005 at 02:30:15AM -0700, Claude Rubinson wrote:
> On Tue, Apr 12, 2005 at 12:14:20AM -0700, Stephen Hooper wrote:
> > 
> > > I need to do some research, as I believe that the bottleneck is in
> > > FVWM's pager.  But, in the meanwhile, I'm thinking that I'd simply
> > > like to check the system first; if the system is currently being used
> > > (possibly indicated by system load; more likely indicated by if the
> > > keyboard or mouse is being used/has been used in the past N seconds)
> > > then don't switch the background.  But I have no clue how I would do
> > > this.
> > 
> > I don't know how well this will work for you, but if you look at
> > your /proc/interrupts do you see an i8042 listed (I actually have two
> > i8042's listed, and the other seems to be mouse related (as I am using a
> > PS/2 mouse)).
> 
> That's the trick!  The i8042 is the keyboard and mouse controller.
> One of the interrupts is for the keyboard, the other for the mouse.  I
> hacked up the below script to track time of last keyboard/mouse event.
> I can then check $LOGFILE to find out how long it's been since I've
> touched the keyboard or moved the mouse and only switch the background
> if it's been more than N seconds.  (I'll have to figure out a decent
> time period by trial and error.)  I have no idea of how portable this
> script is; probably not very.
> 
> I hadn't thought about it, but as Michael first mentioned,
> screensavers do what I'm trying to do.  Reviewing the xscreensaver
> faq, I realized that what the above technique won't do is prevent the
> background from changing when I'm watching a DVD (something that only
> happens rarely; when I'm flying).  But this is a problem that
> xscreensaver has as well, so I don't feel bad.  So, what I might do is
> write another file that indicates whether or not it's safe to switch
> the background.  Before starting a DVD, I simply switch the contents
> of that file from 0 to 1.  (As if I'll ever remember to do that.
> Maybe a wrapper around mplayer.)
> 
> This latter solution also opens up the possibility of completely
> reworking my background rotation scheme.  Rather than switching
> backgrounds at a specified interval, I could take a screensaver
> approach and switch backgrounds during times of low activity.  Might
> be fun.

With the end of the semester chaos, I forgot to follow up on this.  As
a refresher, I was looking for a way to switch my X background when I
wasn't using the system.  Stephen figured out that monitoring the
keyboard/mouse controller was the key and I whipped up a short script
as a proof-of-concept.  The final solution was also pretty short; I've
included it below.  (There's actually no need to write the
keyboard/mouse event value to a file as I was doing originally.)  A
more flexible solution would specify the two delays and the action on
the commandline, but this serves my purposes.  I've been using this
solution for a few weeks now and it seems to work fairly well, I've
only collided with the background switching script once during that
time.

Thanks for all of the help!

Claude

#!/bin/bash

# Execute specified event when system is idle.  In many ways, this
# script follows the same logic as a screensaver.  The idea is to
# execute resource intensive commands when the user isn't interacting
# with the system.  Specificially, it waits until the neither the
# keyboard nor the mouse have been used for $N seconds and then
# executes $ACTION.  Setting $INITDELAY restricts the command to
# executing only once every $INITDELAY seconds.

INITDELAY=1800 # 30 minutes
N=300 # 5 minutes
ACTION="background-randomizer"  # custom script that selects a random
				# background from a given directory

function get_event_val {
    grep i8042 /proc/interrupts|awk '{sum+=$2} END{print sum}'
}

while [ true ]; do
    sleep $INITDELAY
    while [ true ]; do
        kme=$(get_event_val)
        sleep $N
        [[ $kme == $(get_event_val) ]] && $ACTION && break
    done
done


More information about the tfug mailing list