Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to write a script that will show how many minutes the user is idle. Since the last touch of the keyboard and mouse. Example:

while true
 sleep 60
 echo $var


The computer/user is idle for 1 minute.

The computer/user is idle for 2 minute.


The computer/user is idle for 5 minute.


Can we do that? It can be in Bash, Python, etc.

share|improve this question

closed as off topic by quanta, Scott Pack, Ward, Sven, petrus Nov 9 '12 at 12:45

Questions on Server Fault are expected to relate to server, networking, or related infrastructure administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

The "w" command should help, from the man page:

w - Show who is logged on and what they are doing.

# w | awk '{if (NR!=1) {print $1,$5 }}'
root 0.00s
share|improve this answer
Thanks. I mean this isn't the end. Pidgin and screensaver know when the last time I touched a computer. I wonder where you collect the data. – My_name Aug 25 '12 at 12:04

Keyboard and mouse is not a clear concept on a multiuser system, where there can be multiple X servers running on the machine, some attached to one or more physical keyboards or mice, some not, or remote X servers where terminals running on the machine connect to, or bash shells running over ssh and plenty more ways for "bash" to take input from a user...

The "xprintidle" will give you the idle time of the X server you're connected to (that X server may be local or remote, even local, it may not necessarily be the currently active one on the system)

The modification time of $TTY will tell you the idle time of the terminal. stat -c %Y "$TTY" is its last modification time in seconds, which you can substract off "$(date +%s)" (if on a GNU system).

share|improve this answer

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