LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   strange request: script to track logged in time (https://www.linuxquestions.org/questions/linux-general-1/strange-request-script-to-track-logged-in-time-58613/)

hamster 05-07-2003 02:31 PM

strange request: script to track logged in time
 
Hi !

I'm having great fun with writing basic bash scripts. It's amazing how specific you can make 'em. I was wondering if anyone could suggest an efficient/reliable way "to keep track of the accumulated time that a user is logged in since the system was set up".

Basically I was think of using the "last" command and using a script to extract the times but this seems unreliable and messy. Another one is to extract the login time and logoff time and add to an accumulating time tracking file.

Just wondering if anyone has other ways of doing this. Thanks!

PTBmilo 05-07-2003 11:58 PM

I've never seen that command before... it's pretty neat.

Something you might want to try is to log the time when a user logs in to a variable, and then subtract that from the current time when they log out, and save that difference in a file somwhere (well, add the difference to the value that is already in that file)

check out the 'expr' command (or use '| bc' ) to do the math.

This can be done from the .bash_profile/.bash_logout files. You can get the time from a number of commands, I used 'date' here:

$ date | sed 's/.* \([0-9]*:[0-9]*:[0-9]*\).*/\1/'
14:49:31

I'm sure there's a bunch of cool options to the 'date' command that will make the format easier for you (absolute time would be nice so that you don't have to worry about the time rolling over @ midnight)

jayakrishnan 05-08-2003 12:19 AM

u can use the 'last' and 'lastb ' command also

hamster 05-08-2003 12:52 PM

Thanks for the info guys!


All times are GMT -5. The time now is 05:53 PM.