Script to determine logged in time


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Script to determine logged in time
# 1  
Old 04-02-2011
Script to determine logged in time

Hi all

So I am thinking my inability to cope with math is bogging me down here so Im asking for help.

I want to determine how long a user has been logged on for by using the date and who commands to determine the time they have been logge don.

My problem is that I keep getting the wrong time. Because of the 60 seconds in a minute. If a user logs on at 14:10 and its curently 15:10 I know I can subtract the two and get one hour.

Where I am stuck is if a user logs on at 14:10 and its now 15:05.

Here is what I have thus far.
Code:
lhour=$(who | grep $1 | cut -c30,31)
lmin=$(who | grep $1 | cut -c33,34)
chour=$(date | cut -c12,13)
cmin=$(date | cut -c15,16)
hrs=$(($chour - $lhour))
if [ $chour -gt $lhour ]
then
min=$((60 - cmin))
hrs=$(($hrs - 1))
else
min=$(($cmin - $lmin))
fi
echo "$1 is logged in for $hrs hours , $min minutes.";

---------- Post updated at 10:42 PM ---------- Previous update was at 10:24 PM ----------

Ah think I got it!

Code:
lhour=$(who | grep $1 | cut -c30,31)
lmin=$(who | grep $1 | cut -c33,34)
chour=$(date | cut -c12,13)
cmin=$(date | cut -c15,16)
if [ $chour -ge $lhour ]
then
hrs=$(($chour - $lhour))
else
hrs=$((($lhour - $chour) + 24))
fi
if [ $cmin -gt $lmin ]
then
min=$(($cmin - $lmin))
else
min=$((60 - ($lmin - $cmin)))
fi
 
echo "$1 has been logged in for $hrs hours and $min minutes.";

# 2  
Old 04-02-2011
The script will have problem, if the user are logining two or more times.

try epoc time, it will save your coding.
# 3  
Old 04-02-2011
I never heard of epoch time till just n ow. I went and read up on it but it seems FAR more confusing and I dont really see how exactly it would help me in this situation. You would need to be able to pull the logged in time, convert it to epoch, take the current time, convert it, then subtract the two, convert it BACK and display it. Cant really find documentation on this that doesnt relate to PERL. Im using BASH. Do you ahve any good references on it?

Thanks
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

• Write a shell script that upon invocation shows the time and date and lists all the logged-in user

help me (1 Reply)
Discussion started by: sonu pandey
1 Replies

2. Shell Programming and Scripting

Determine previous time in minutes

I have several logs with where the time stamp in the logs are "YYYYMMDDHHMM". I would like to check the last line in each file to make sure the entry is less than 5 minutes old. My timezone is EST5EDT so the following will work for 1 hour. But I need something easy for 5 minutes ago.... (5 Replies)
Discussion started by: oldman2
5 Replies

3. Shell Programming and Scripting

Shell script to display user logged last week and time connected

Hello all, I want to display all the user logged last week and know the time tnat they are connected. I have been looking for a solution in the Forum, but I didn't find. Thanks in advance (4 Replies)
Discussion started by: ahernandez
4 Replies

4. UNIX Desktop Questions & Answers

Shell script to display user logged in within given time

how can i know which users have logged in at specified given start and end time in 24 hour format? (3 Replies)
Discussion started by: meherzad4u
3 Replies

5. UNIX for Dummies Questions & Answers

Finding users logged on time and space useage

I need to find what users are currently logged onto the system that is easy just a simple who | awk '{ print $1 }' (thats all I need for the part), but I also need to find how long they have been logged on and the total amount of file space they are using. Thanks in advance, I have been looking... (3 Replies)
Discussion started by: mauler123
3 Replies

6. UNIX for Dummies Questions & Answers

How to do a login time and date list of users never logged on?

Hello, I'm trying to do a list of user that never connected to a couple of servers. I want to do a diff between the servers lists, and print out only the users that never has logged on each server. Here my first step : SERVER01: # finger `egrep -v -e "^\s*#" /etc/passwd | awk '{ print $1 }' |... (4 Replies)
Discussion started by: gogol_bordello
4 Replies

7. UNIX for Dummies Questions & Answers

How many time have user logged last X days?

Hi all.. I was trying to do a little shell script, that would list users and their login times, lets say like last 5 days. But I couldnt figure out how to count users login times from previous days. Any tips? Funny that nobody has do this kinda script before, or atleast I couldnt find on... (2 Replies)
Discussion started by: Kimmo_
2 Replies

8. Shell Programming and Scripting

How can one know how much time user logged?

Hello, i know who command gives you the time when particular user logged in. And subtracting today's date and time from the one found in who we can get how much time user logged in. But this can get very much clumsy as we can't subtract date directly in unix . Is there any other way or command... (4 Replies)
Discussion started by: salman4u
4 Replies

9. Shell Programming and Scripting

Last time logged in

Working in AIX (so no date -d) How can i display all the users who have not logged in for more than 40 days? A small quick script would be usefull, my scripts are always taking to long to execute, even before they are finished. Many thanks! (5 Replies)
Discussion started by: ughosting
5 Replies

10. UNIX for Dummies Questions & Answers

Finding last time users logged in

I would like to find out the last time all users have logged in or out. I tried the last command, but it could not find the wtmp file in /var/adm (I searched in othe directories also). This is an AIX rs6000 4.2.1 system. We are moving our applications from this system to an AIX 5.2 system and I... (11 Replies)
Discussion started by: jyoung
11 Replies
Login or Register to Ask a Question