Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 02-22-2013, 10:34 AM   #1
LQ Newbie
Registered: Dec 2012
Posts: 9

Rep: Reputation: Disabled
Display only logins with an inactivity period of 3 hours or more using who command

My problem is this:

I need to be able to display all logins that have been inactive for more than 3 hours using the who command. How can I accomplish this?

This is what I have so far:

who -uH | grep -v 0: | pg
This results in a list similar to the one below:

root       vty1    May  9 10:50    old     14680242
someuser   pts/0   Feb 19 13:45    1:24    31916048   (
someuser2  pts/1   Feb 15 14:43   17:43    45154356   (
someuser3  pts/11  Feb 20 15:09     .      22347826   (
someuser4  pts/25  Feb 20 07:42   16:58    30146784   (
someuser5  pts/34  Feb 22 07:50     .      20381822   (
With the above command, I see logins and activity under 1 minute; greater than 59 minutes. I only need to see inactivity of 3 hours or more.

Can someone help?
Old 02-26-2013, 07:01 AM   #2
Registered: May 2001
Posts: 29,358
Blog Entries: 55

Rep: Reputation: 3545Reputation: 3545Reputation: 3545Reputation: 3545Reputation: 3545Reputation: 3545Reputation: 3545Reputation: 3545Reputation: 3545Reputation: 3545Reputation: 3545
IMHO the easiest way to do time calculations is to convert time stamps to epoch first. Apart from caveats I don't want to think about like TZ, idle time > 24hrs, idle time > 'date --date="1 year ago";' which source it uses and what should happen on utmp/wtmp rotation/cleanup, time stamps don't show '%Y' and the date format is a human readable '%b %e %H:%M' one. So deal with strings like "old" in the idle time column, convert the start time and idle time to epoch and then check if idle time minus start time equals or is greater than three hours ago. Kludgy, not much fun but doable.
Old 02-27-2013, 08:50 AM   #3
Registered: Nov 2011
Location: Cairo, Egypt
Distribution: CentOS, RHEL, Fedora
Posts: 300
Blog Entries: 2

Rep: Reputation: 51
If you could use w command, it can give you better results too.
It shows JCPU & PCPU time based on process used by users. It can give you desired redults too.
But if you want to use who command.
use who -a. it gives you all information about logged in and logged out users.
if exit=0, it means users who are logged out.
You can find the list of logged in users who do not have exit=0

You can use last command for the list of logged in and logged out users.
Here you can find out the users who were logged in more than three hours but currently logged out.
But if you want to see details of logged in users, you can use
$ last | grep "Feb 27" | grep "still"
It will show the current logged in users of 27 feb with there logged in time.
Suppose it is 20:00 pm and you want to know the users who were logged in around 18:00 pm and all after that, you can use
 last | grep "Feb 27" | grep "still" | grep -e "18:" -e "19:" -e "20:"
It will show you list of all users who were logged in after 18:00 pm and still logged in.

Last edited by fortran; 02-27-2013 at 09:31 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Screen saver does not start after period of inactivity statguy Slackware 3 02-27-2009 07:52 PM
Blank Screen after short period of inactivity SweetLou Debian 6 12-13-2006 08:45 PM
How do I automatically logout after a period of inactivity? AshleyK Linux - Security 4 12-31-2004 10:47 PM
running a command after a set period of keyboard inactivity? Nocterro Linux - Software 3 09-03-2004 09:04 AM
Log entry during period of inactivity Gavin Linux - General 1 01-14-2002 01:08 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:37 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration