View Full Version : Check for user activity in a bash script

July 24th, 2008, 06:01 PM
I'm writing a bash script which will be run as a cron-job, since the script makes the computer rather sluggish during its execution I'd like to find some way of checking wherever someone is actively using the computer or not.

So if anyone knows how to check if the keyboard/mouse is being used or have some better suggestion I'd be grateful for the input.

Also I should state that I'm aware that I could check if any of the user accounts are logged on fairly simple, however this doesn't cover the likely situation that a user is logged on but not active.

July 24th, 2008, 07:12 PM
A better way in my opinion is to run the script with the lowest possible priority.

nice -n 19 ./sluggish-script
Read the manpage of nice.

July 24th, 2008, 09:25 PM
What I neglected to say is that the sluggishness stems from extensive disk access rather than CPU usage. Therefore nice won't help me. As long as one aren't accessing the disk the script is unnoticeable however should one desire to access a file the program stalls for several seconds.

July 24th, 2008, 11:02 PM
/proc/interrupts contains, among other things, the number of interrupts generated by the keyboard and mouse. So if you are able to find them, you can check for any difference and hence user activity.

Otherwise, if you want to check disk activity you can use iostat or read directly /proc/diskstats