Connected time on UNIX

Hello,

A question please.

Could anyone tell me how to know the connected time for a user on unix ?

I'd need to know this information for several days.

Is there a command to get it ?

Thanks in advance

For an interactive shell with a terminal:

who am i

Further you can query the current shell process for elapsed time and start time:

ps -p $$ -o etime
ps -p $$ -o stime

BTW you can suppress the header line with

ps -p $$ -o etime=
ps -p $$ -o stime=

Thanks MadeInGermany.

Do you know how could I query for passed days, for example 18/01 and 19/01 ?

greetings

Hmm, date calculation can become complicated.
GNU date has

date --date "-1 days"
date --date "-2 days"

On standard Unix you should have a look at
Yesterdays Date/Date Arithmetic | Unix Linux Forums | Answers to Frequently Asked Questions
An example to check the current process' etime against the two previous days:

etime=`ps -p $$ -o etime=`
case $etime in
(*-*) edays=${etime%-*};;
(*) edays=0;;
esac
if [ $edays -eq 1 -o $edays -eq 2 ]; then
  echo "the current process is 1-2 days old"
fi

NB some of these expressions only work in Posix shells (ksh, bash, zsh).

Did you try the last command?

Hi Rudic,

I tried LAST command, but I only get the users logged in today only, not for passed days.

I am not an experimented guy on unix.

Thanks

man last :

What be the creation date of your /var/log/wtmp?

Hello Rudic,

When I execute :

1) $ last -f /var/log/wtmpx , because wtmp does not exist
I get
wtmp begins Tue Jan 21 11:55

/var/log/wtmpx was created janvier 21 02:10
It contains 0 bytes now.

I didn't get what am looking for.

2) $ last -f /var/adm/wtmpx
I get
wtmp begins Tue Jan 21 08:04

3) $ last -f /var/adm/WTMPX020 ---> I try on this file because its date is 19/01/2014
I get
wtmp begins Tue Jan 21 08:04

Thanks in advance
Best regards

Well, looks like wtmp is being deleted/overwritten quite frequently. Any janitor (like log_rotate) or cleanup (maybe at boot time) processes running? Any old wtmp files available?

Hi Rudic,

I am sorry... I commited errors typing the command...

$ last -f /var/adm/WTMPX020 ---> I try on this file because its date is 19/01/2014

That command works properly. There is a different file for each day.

Thanks you, Rudic and MadeinGermany for your help :slight_smile: