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 ...)


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


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.

| 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

| 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
  • 5
    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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.