Finding out active user hours on a system
Consider a web server with shared hosting. Many users log in and log out to the server every day and the user activity gets logged in the server's system log. This recipe is a practice task to make use of the system logs and to find out how many hours each of the users have spent on the server and rank them according to the total usage hours. A report should be generated with the details, such as rank, user, first logged in date, last logged in date, number of times logged in, and total usage hours. Let's see how we can approach this problem.
Getting ready
The last
command is used to list the details about the login sessions of the users in a system. The log data is stored in the /var/log/wtmp
file. By individually adding the session hours for each user, we can find out the total usage hours.
How to do itβ¦
Let's go through the script to find out active users and generate the report:
#!/bin/bash #Filename: active_users.sh #Description: Reporting tool to...