giampaolo / psutil

Cross-platform lib for process and system monitoring in Python
BSD 3-Clause "New" or "Revised" License
10.07k stars 1.37k forks source link

Get idle time of users #1115

Open nelson-liu opened 6 years ago

nelson-liu commented 6 years ago

Hi, It'd be great to add something to psutil.users() that would encode idle time, as one would get from the output of running the w command.

Thanks, this is a very useful library!

giampaolo commented 6 years ago

Hello there! users() function already provides a started field, which is basically the login time expressed in seconds since the epoch. What would "idle" time represent exactly?

nelson-liu commented 6 years ago

Hi! Thanks for your response. I don't think the idle time is quite the same as login time since the epoch. Idle time would be quite low whenever a user is actively using the shell / remote session, but if they step away then it would start ticking. This link provides a good explanation of how the w command calculates idle time.

giampaolo commented 6 years ago

Mmmm I see. Judging from some google searches I've just tried it seems there is some demand for this (or something similar to this). I'm not entirely clear what we should return though. Are we looking at the cumulative idle time as in "for how long the user has been idle in total (secs)"?

giampaolo commented 6 years ago

...or should this be a "counter" instead which resets back to 0 once the user types something in the terminal?

In fact this poses questions on whether we should only consider the terminal or also the X session: https://askubuntu.com/questions/202136/how-can-a-script-detect-a-users-idle-time ...so whatever we end up implementing we must be clear on what exactly we're providing.

nelson-liu commented 6 years ago

Good question! I think that it should be a counter, and that it should consider both terminals and X sessions (i believe the w command does this as well).