I'm trying to figure out a process that monitors users sessions on a remote server and alerts them when they are being idle too long, which with the Linux command w
is aptly appropriate.
Problem is - w
uses 3 different formats to specify the idle time of the session, and I can't figure them out properly. An output of w
might look like this:
11:40:57 up 400 days, 10:46, 13 users, load average: 5.07, 5.10, 4.83
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
john pts/1 XX.XX.XX.XX Wed13 22:29m 0.13s 0.04s ssh master-db
june pts/2 XX.XX.XX.XX Wed13 46.00s 0.67s 0.13s -bash
jenn pts/4 XX.XX.XX.XX 11:13 27:47 4.16s 0.11s -bash
As you can see, IDLE has different formats for each of the users:
- "AA.BBs" obviously means that AA seconds and BB 1/100ths of a second (46 seconds in the case of June) has passed since she was last active on the console.
- "AA:BBm" probably means that AA hours and BB minutes have passed since John was last active on his session.
- "AA:BB" is the format I can't figure out - how long has Jennifer not being active in her session?
From the man page
so your output is MM:SS (>1m and <1 hour).
Without a qualifier, it means MM:SS -- that is, minutes and whole seconds. As an added bonus, there's a fourth format you don't have in that output -- a number of days (NNdays) of inactivity.
The answer is 27minutes and 47seconds
Not quite the answer to your question, but an easier approach to checking the idle time of login sessions would be to look at /dev/pts. The modification times of the files in there reflect the last time the login session received input.
You should be able to do stat operations there (e.g., stat --format="%n %X" *), and keep everything in epoch seconds. Should make any time calculations easier.