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 want to check, inside a bash script (*), how long the user of a X session has been idle.

The user himself does not have to be using bash, but just X. If the user just moved the mouse, for example, a good answer would be "idle for 0 seconds". If he has not touched the computer in 5 minutes, a good answer would be "idle for 300 seconds"

The reason to not use xautolock straight away is to be able to implement some complex behavior. For example, if the user is idle for 10 minutes, try to suspend, if he is idle for more 5 minutes, shutoff (I know it sounds odd, but suspend does not always work here ...)

(*)or could be another language.

share|improve this question

2 Answers 2

up vote 14 down vote accepted

Just found a simple way to do it.

There is a program called xprintidle that does the trick

getting the idle time (in milliseconds) is as simple as

xprintidle

and to install

apt-get install xprintidle

For sysadmins, it also works remotely

From an ssh session:

export DISPLAY=:0 && sudo -u john xprintidle

where john is the user logged into the X session on the remote machine.

share|improve this answer

Answer from here:

In bash

w | tr -s " " | cut -d" " -f1,5 | tail -n+3

gives you a username/idletime pair for each shell. So basically you can get the idle information through the command w

share|improve this answer
    
how does this count interactions with X that are not through a terminal ? Like, say a mouse movement inside a firefox. If the user is moving the mouse, I'd like to get the answer "not idle", or "idle for 0 seconds" –  josinalvo Oct 17 '12 at 17:06
    
@josinalvo w just gives you the idle time in that particular terminal I think. –  Dan Oct 17 '12 at 17:16
    
That idle time is in fact the running time, the uptime of the process (WHAT column of the w output). So it gives no idea about how long the user has been idle in his X session. –  rosch Oct 17 '12 at 17:55

Your Answer

 
discard

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.