7.2.2 Determining Who is Logged On

7.2.2  Determining Who is Logged On

Let us assume that we are running Unix under C-shell or csh. In a workstation, a person may have several windows open, each window running a copy of the C-shell program. In addition, a person may be logged in several workstations. If we want to count the number of times a person is logged in or is running C-shell, we can run a program like the one that follows.

 Program 7.7

#!/usr/bin/perl

#prints out the people who are logged in 
use strict;

open (PROCESSES, "ps -aux |");

#Assuming csh is the shell used, it shows all who are logged in.
#We can do more stuff such as log out those who have been idle for 
#a long time, if we have the privilege to do so.
while (){
    if (/csh/) {print STDOUT;}
}
close (PROCESSES);

The output of this program for one run is given blow.

% csh.pl 
kalita   10693  0.0  0.4  2656 1252 pts/9    S    15:39   0:00 perl csh.pl