cessprin has asked for the wisdom of the Perl Monks concerning the following question:

My question is probably more Unix related, but I need to incorporate it in a Perl script. I was wondering if there's a built-in function (Unix or Perl) that displays how long a user has been logged? I tried using `time`, which shows the elapsed time since logon in the third field, but when I try using `time | cut -d" " -f3` to cut that field from the output, I get '0:00.00' if run on the command line, and 'usage: time [-p] utility [argument...]' if run in the Perl script. I appreciate any help.

Replies are listed 'Best First'.
Re: Login time
by betterworld (Curate) on Apr 15, 2007 at 21:23 UTC
    You may extract this information from the utmp file (see its man page for further information). (I don't know if every Unix variant has that file, though.)
Re: Login time
by ysth (Canon) on Apr 15, 2007 at 21:26 UTC
    "time" executes a command and shows how long it took to run. Try "last".
Re: Login time
by lidden (Curate) on Apr 15, 2007 at 23:53 UTC
    Using the module suggested by derby.
    perl -M"User::Utmp':constants',':utmpx'" -wle '$t=time;print $_->{ut_u +ser}, " : ", ($t-$_->{ut_tv}{tv_sec})/3600/24, " days." for getutx'