Activate command when computer is idle

I would like to run a command if my computer is idle.

I found this, but is not quite working.

#!/bin/sh
idletime=$(xprintidle)
threshold=300000 # 5 min = 5 * 60 * 1000 ms
if [ "$idletime" -gt "$threshold" ]; then
  echo "idle for 5 minutes."
fi

Hi,

I think there are two main questions here: firstly, when you say that it "is not quite working", what is going wrong ? Are you getting an error, or do you find that the idle message is never displayed ? If you can give some detail on what precisely is gong wrong and how you're determining that it's gone wrong, that will be helpful.

Secondly: what is it you're intending to do once the idle threshold is reached ? I ask because it may well be that other utilities already exist to detect and perform your idle task without you having to re-invent the wheel. For example, if you simply wish to lock your X session, then things like xscreensaver or xlock (or its various successors) may well do the job without you having to worry any more about it. So if you can explain why you need to detect the system being idle and what you plan on doing when it is, there may be other solutions here aside from writing something yourself.

For example, I would like to terminate Seamonkey if there has been no mouse or keyboard input.

The script never executes the echo statement.

If you aren't typing the command to invoke your script (thereby providing keyboard input), how are you invoking your script?

If you have it set up as a cron job, is xprintidle on your system's default search path? And, if so, there is no terminal associated with a cron job. Did you check your mailbox for output produced by your cron job?

Yes, I was invoking script by typing it.

Using a cron job involves a complexity that I would like to avoid.

You have brought up some good points and I need to do some more thinking. :slight_smile: