1
reboot   system boot  3.10.0-327.el7.x Wed Oct 26 15:12 - 22:43  (07:30)    
root     :0           :0               Wed Oct  5 05:01 - 05:31  (00:29)    
(unknown :0           :0               Wed Oct  5 05:01 - 05:01  (00:00)    
reboot   system boot  3.10.0-327.el7.x Tue Oct  4 23:01 - 05:31  (06:29)    
root     :0           :0               Wed Oct  5 04:56 - 04:58  (00:01)    
(unknown :0           :0               Wed Oct  5 04:56 - 04:56  (00:00)    
reboot   system boot  3.10.0-327.el7.x Tue Oct  4 22:55 - 04:58  (06:02)  

Anyone can show me how i can write a command in linux that counts the number of logins each user did and calculate the total number of time spent. Project it onto the screen that shows something this.

User root logged in a total of ?? times with a total of mins/hours logged
User unknown logged in a total of ?? times with a total of mins/hours logged.
  • 2
    Looks suspiciously like a homework assignment. – user207673 Feb 2 '17 at 8:03
  • What have you tried so far so we can get an idea of just how much help you need. – Tigger Feb 2 '17 at 8:06
  • This provides the total of logins for every user: for i in $(awk '{print $1}' login_file.txt | sort -u); do echo -e $i: ; grep $i login_file.txt | wc -l; done, from there try to get your desired outputs and ask if you have any specific question. – Zumo de Vidrio Feb 2 '17 at 8:13
0
reboot   system boot  3.10.0-327.el7.x Wed Oct 26 15:12 - 22:43  (07:30)    
root     :0           :0               Wed Oct  5 05:01 - 05:31  (00:29)    
(unknown :0           :0               Wed Oct  5 05:01 - 05:01  (00:00)    
reboot   system boot  3.10.0-327.el7.x Tue Oct  4 23:01 - 05:31  (06:29)    
root     :0           :0               Wed Oct  5 04:56 - 04:58  (00:01)    
(unknown :0           :0               Wed Oct  5 04:56 - 04:56  (00:00)    
reboot   system boot  3.10.0-327.el7.x Tue Oct  4 22:55 - 04:58  (06:02) 

considering the above detail is in a file:

for i in `awk '{print $1}' test_login |sort|uniq`;
 do
         count=`grep -c $i test_login`
        sum=0;
        for j in `grep $i test_login|awk '{print $NF}'|cut -c 2-6`;
         do
                 var=`echo $j | awk -F: '{print ($1 * 60) + $2}'`;
                 sum=$((sum+var));
        done
        ((hour=$sum/60))
        ((min=$sum-$hour*60))
        echo "User $i logged in a total of $count times with a total of $hour:$min logged"
  done

test_login : file which contains log of all users.

  • What have i done wrongly. I am using last for capturing of the logins instead of a file. – David Goh Feb 5 '17 at 3:31
  • ./totallog.sh: line 4: 5: command not found User itdp04 logged in a total of times with a total of 47:5 logged ./totallog.sh: line 4: 31: command not found User reboot logged in a total of times with a total of 458:22 logged ./totallog.sh: line 4: 540: command not found User root logged in a total of times with a total of 136:8 logged ./totallog.sh: line 4: 31: command not found User (unknown logged in a total of times with a total of 2:17 logged ./totallog.sh: line 4: 1: command not found User wtmp logged in a total of times with a total of 16:0 logged – David Goh Feb 5 '17 at 3:32
  • #!/bin/bash for i in last |gawk '{print $1}' |sort|uniq do count= last |grep -c $i sum=0; for j in last |grep $i |gawk '{print $NF}'|cut -c 2-6; do var=echo $j | awk -F: '{print ($1 * 60) + $2}'; sum=$((sum+var)); done ((hour=$sum/60)) ((min=$sum-$hour*60)) echo "User $i logged in a total of $count times with a total of $hour:$min logged" done – David Goh Feb 5 '17 at 3:32
  • Thanks for your help. I managed to get it to work with last with your codes. – David Goh Feb 5 '17 at 4:58
  • Glad it worked. If helped, mark question as answered. – Rakesh.N Feb 5 '17 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.