Track user log!

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    The task is to measure the density of users that are logged on system. The program
    should check that every 30 sec, but not check older logs than 2 days(24h) and write that into a text file. So the logs shouldn't be older than 2 days. The records of logged users must be saved in a file for today and what was going on yesterday.
    And the time difference MUST be 30 seconds between checkings...

It should look like this:
Time: 11:50:10 Users: 2
Time: 11:50:40 Users: 5

1.file for today Today.txt
2.file for yesterday logs Yesterday.txt

  1. Relevant commands, code, scripts, algorithms:
    ??

  2. The attempts at a solution (include all code and scripts):
    I'm thinking about the function who -Hlrmu and to cat that into a file
    than read how many lines a file has minus the first one.

#!/bin/bash

while true; do
sleep 30
users=$(who -Hlrmu | cat >> record.txt | wc -l record.txt | cut -c1-2)
echo -e "Time: ???? Users:$users\n"

done
exit

I don't know how to measure the time and to separate the files into two,
one for today and one for yesterday.

Am I on the right path? I'm not i know hahaha

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    University of Ljubljana, Ljubljana, Slovenia, Janez Novak, ID63709

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Everyone at the UNIX and Linux Forums gives their best effort to reply to all questions in a timely manner. For this reason, posting questions with subjects like "Urgent!" or "Emergency" and demanding a fast reply are not permitted in the regular forums.

For members who want a higher visibility to their questions, we suggest you post in the Emergency UNIX and Linux Support Forum. This forum is given a higher priority than our regular forums.

Posting a new question in the Emergency UNIX and Linux Support Forum requires forum Bits. We monitor this forum to help people with emergencies, but we do not not guarantee response time or best answers. However, we will treat your post with a higher priority and give our best efforts to help you.

If you have posted a question in the regular forum with a subject "Urgent" "Emergency" or similar idea, we will, more-than-likely, close your thread and post this reply, redirecting you to the proper forum.

Of course, you can always post a descriptive subject text, remove words like "Urgent" etc. (from your subject and post) and post in the regular forums at any time.

Thank you.

The UNIX and Linux Forums

Hmm but i must post a homework in a correct forum, and my homework is in a rush, so
i still can post a homework in the Emergency UNIX and Linux Support !! Help Me!! - The UNIX and Linux Forums ???

just read the rules and don't use words like "urgent" in your title.

Ok i will, can you help me with the problem i have?

---------- Post updated 28-05-10 at 02:03 PM ---------- Previous update was 27-05-10 at 08:18 PM ----------

Does the who -m lists users who are on the system or just me?

would it be better if i used the command users or simply who ?

You can use who to get the number of users logged in your system...

who -m will show you "only hostname and user associated with stdin"

man who

Try this:

You'll need to adjust the output redirection and maybe the cron schedule.

Aha, what if i use $(users | wc -l)
coz, who returns me on two consoles tty7 and pts/0

How can i separate the files, the Today.txt must contain users who were logged in today
and Yesterday.txt must contain yesterday logs, and i should have no more than 2 days old logs.

Something like if [ $(date +%Y%m%d).log -ge than 48h ]; than delete it?
How can i tell if a file is older than 2 days?

---------- Post updated at 03:58 PM ---------- Previous update was at 02:40 PM ----------

it would be even better if i rewrite from today.txt into yesterday.txt, but only when
24 hours passes and the today.txt will be blank.
What i mean is...i must keep track of the users for 24 h and then rewrite it
to a yesterday.txt file!:slight_smile:

How is that doable???

#! /bin/sh
[ $(date "+%H")-eq 0 ] && (mv path/to/today.txt /path/to/yesterday.txt)
echo "Time: $(date +%T) Users: $(who | wc -l)" >> /path/to/today.txt

Run this script thru crontab every 10 seconds.