User Idle time in Linux

我们两清 提交于 2021-02-10 06:19:11

问题


I need to check how much time passed since last user input occurence (preffered way - in python) on Linux (lucid - 10.4)

I know that this is easy in normal way (just using XScreenSaverQueryInfo), but the tricky part is that I don't have x11/extensions/scrnsaver.h header and I HAVE to do that some other way (even if I install needed package I cannot install packeges on 100 other computers on which it will work - I don't have permission to do that).


回答1:


In deps of internet I found something like this, and it looks like working - but still not perfect solution because it returns much more info than I need and also the number near which is proper value is not constant (it differs in other system):

ls -l /dev/pts | fgrep username




回答2:


using xprintidle

requirements: sudo apt-get install xprintidle

usage from command line: xprintidle

or, in seconds: echo $((`xprintidle` / 1000))

or from python:

milliseconds_string = os.popen("xprintidle").read()
seconds = int(milliseconds_string)/1000



回答3:


Another solution would to be use the unix command w, this gives the idle time of all users (with the exception of the w command which appears to not reset idle time)

the w command also has a few flags -s and -f to show a short format and not print the hostname giving you slightly less information to find the idle time. I was able to get it down to

> w -sf | grep user
jxw1234  pts/5      6.00s w -sf

This also gives idle time down to seconds so is more precise than ls -l /dev/pts.

From this point it should be pretty simple to run the command using python's system interface and apply a regex to pull out the time in various formats.

EDIT: Using this w command get idle time i was able to get it down to the following result.

>w | awk '{if (NR!=1) {print $1,$5 }}' | grep username
jxw1234 1.00s


来源:https://stackoverflow.com/questions/15068887/user-idle-time-in-linux

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!