利用perl检测系统中登陆用户相关的进程升级版

#!/bin/perl
open(FH,"user")or die "cant open file:$!";
@lines=<FH>;
chomp(@lines);
close FH;
#print "@lines\n";
open(PASSWD,"/etc/passwd")or die "cant open:$!";
foreach $username (@lines){
#print "$username.\n";
while($pwline=<PASSWD>){
        if($pwline =~/$username:/){print "$username is a system user here\n";last;}
        else {die "$username is not a system user here\n";}
}
close PASSWD;
open(LOGGEDON,"who|")or die "cant open:$!";
while($logged=<LOGGEDON>){
if($logged =~ /$username/){$logged_on=1;last;}
}
close LOGGEDON;
die "$username is not logged on.\n" if !$logged_on;
print "$username is logged on and running these processed.\n";
open(PROC,"ps aux|") or die "cant open: $!";
while($line=<PROC>){
print "$line" if("$line" =~/^$username/);
}
close PROC;
print "*"x 10,"\n";
print "so long.\n";
print "\n\n";
}
 

用户可以根据自己的需要再添加修改,比如,可以打印每个用户有几个进程正在运行,占用内存等等。

 

你可能感兴趣的:(linux,用户,运维,perl,进程)