Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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:

#!/bin/bash
while true
do
 command_lines...
 ...
 sleep 60
 echo $var
done

Result:

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, SvW, petrus Nov 9 '12 at 12:45

Questions on Server Fault are expected to relate to professional 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.

add comment

2 Answers

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 }}'
USER IDLE
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
add comment

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
add comment

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