📄 linux-lib.pl
字号:
# linux-lib.pl# Functions for parsing linux ps outputsub list_processes{local($pcmd, $line, $i, %pidmap, @plist, $dummy, @w, $_);if (`ps -V` =~ /version\s+2\./) { # New version of ps, as found in redhat 6 foreach (@_) { $pcmd .= " -p $_"; } if (!$pcmd) { $pcmd = " -e"; } open(PS, "ps -o user,ruser,group,rgroup,pid,ppid,pgid,pcpu,vsz,nice,etime,time,tty,args $pcmd |"); $dummy = <PS>; for($i=0; $line=<PS>; $i++) { chop($line); $line =~ s/^\s+//g; @w = split(/\s+/, $line); if ($line =~ /ps -o user,ruser/) { # Skip process ID 0 or ps command $i--; next; } $plist[$i]->{"pid"} = $w[4]; $plist[$i]->{"ppid"} = $w[5]; $plist[$i]->{"user"} = $w[0]; $plist[$i]->{"cpu"} = "$w[7] %"; $plist[$i]->{"size"} = "$w[8] Kb"; $plist[$i]->{"time"} = $w[11]; $plist[$i]->{"nice"} = $w[9]; $plist[$i]->{"args"} = @w<14 ? "defunct" : join(' ', @w[13..$#w]); $plist[$i]->{"_group"} = $w[2]; $plist[$i]->{"_ruser"} = $w[1]; $plist[$i]->{"_rgroup"} = $w[3]; $plist[$i]->{"_pgid"} = $w[6]; $plist[$i]->{"_tty"} = $w[12] =~ /\?/ ? "None" : "/dev/$w[12]"; } close(PS); }else { # Old version of ps $pcmd = join(' ' , @_); open(PS, "ps aulxhwwww $pcmd |"); for($i=0; $line=<PS>; $i++) { chop($line); if ($line =~ /ps aulxhwwww/) { $i--; next; } if ($line !~ /^\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+([\-\d]+)\s+([\-\d]+)\s+(\d+)\s+(\d+)\s+(\S*)\s+(\S+)[\s<>N]+(\S+)\s+([0-9:]+)\s+(.*)$/) { $i--; next; } $pidmap{$3} = $i; $plist[$i]->{"pid"} = $3; $plist[$i]->{"ppid"} = $4; $plist[$i]->{"user"} = getpwuid($2); $plist[$i]->{"size"} = "$7 Kb"; $plist[$i]->{"cpu"} = "Unknown"; $plist[$i]->{"time"} = $12; $plist[$i]->{"nice"} = $6; $plist[$i]->{"args"} = $13; $plist[$i]->{"_pri"} = $5; $plist[$i]->{"_tty"} = $11 eq "?" ? "None" : "/dev/tty$11"; $plist[$i]->{"_status"} = $stat_map{substr($10, 0, 1)}; ($plist[$i]->{"_wchan"} = $9) =~ s/\s+$//g; if (!$plist[$i]->{"_wchan"}) { delete($plist[$i]->{"_wchan"}); } if ($plist[$i]->{"args"} =~ /^\((.*)\)/) { $plist[$i]->{"args"} = $1; } } close(PS); open(PS, "ps auxh $pcmd |"); while($line=<PS>) { if ($line =~ /^\s*(\S+)\s+(\d+)\s+(\S+)\s+(\S+)\s+/ && defined($pidmap{$2})) { $plist[$pidmap{$2}]->{"cpu"} = $3; $plist[$pidmap{$2}]->{"_mem"} = "$4 %"; } } close(PS); }return @plist;}# renice_proc(pid, nice)sub renice_proc{$out = `renice $_[1] -p $_[0] 2>&1`;if ($?) { return $out; }return undef;}# find_mount_processes(mountpoint)# Find all processes under some mount pointsub find_mount_processes{local($out);$out = `fuser -m $_[0]`;$out =~ s/[^0-9 ]//g;$out =~ s/^\s+//g; $out =~ s/\s+$//g;return split(/\s+/, $out);}# find_file_processes([file]+)# Find all processes with some file opensub find_file_processes{local($out, $files);$files = join(' ', @_);$out = `fuser $files`;$out =~ s/[^0-9 ]//g;$out =~ s/^\s+//g; $out =~ s/\s+$//g;return split(/\s+/, $out);}# get_new_pty()# Returns the filehandles and names for a pty and ttysub get_new_pty{opendir(DEV, "/dev");local @ptys = map { "/dev/$_" } (grep { /^pty/ } readdir(DEV));closedir(DEV);local ($pty, $tty);foreach $pty (@ptys) { open(PTY, "+>$pty") || next; local $tty = $pty; $tty =~ s/pty/tty/; open(TTY, "+>$tty") || next; local $old = select(PTY); $| = 1; select(TTY); $| = 1; select($old); return (*PTY, *TTY, $pty, $tty); }return ();}foreach $ia (keys %text) { if ($ia =~ /^linux(_\S+)/) { $info_arg_map{$1} = $text{$ia}; } elsif ($ia =~ /^linuxstat_(\S+)/) { $stat_map{$1} = $text{$ia}; } }@nice_range = (-20 .. 20);$has_fuser_command = 1;1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -