Penguin
Diff: PerlOneLiners
EditPageHistoryDiffInfoLikePages

Differences between version 13 and predecessor to the previous major change of PerlOneLiners.

Other diffs: Previous Revision, Previous Author, or view the Annotated Edit History

Newer page: version 13 Last edited on Thursday, April 8, 2004 5:11:21 pm by JohnMcPherson Revert
Older page: version 12 Last edited on Tuesday, December 16, 2003 9:24:05 pm by AristotlePagaltzis Revert
@@ -27,7 +27,15 @@
  
  perl -pe 's!\\n!\n!g; s!\\t!\t!g' $file 
  
 You can use any punctuation as the separator in an __s///__ command, and if you have backslashes or even need literal slashes in your pattern then doing this can increase clarity. 
+  
+!! List all currently running processes  
+ perl -pe 'BEGIN {undef$/;chdir"/proc";@ARGV=sort{$a<=>$b}glob("*/cmdline")}  
+ $ARGV=~/(\d+)/;print "$1\t";s@\0@ @g;$_.="\n";'  
+  
+This prints the process ID and command line of every running process on the system (except some "special" kernel processes that lie about/don't have command lines). You might want such a command if you suspect a rootkit or something similar has been installed, and you can't trust your "ps" binary.  
+  
+Basically, this runs a loop over all the files in /proc/*/cmdline, printing the content of those files (after printing the leading digits in the filename, and replacing null characters with a space). "$/" is a special variable used for the end-of-line marker, and it needs to be unset so that even the empty/unreadable files still cause the loop to print out the filename. The __-p__ switch for perl means do the loop, using the input of the rest of the arguments (assumed to be filenames). We cheat and manually assign @ARGV in the BEGIN{..} block. Also, we need to tell sort to use a numeric comparison (<=>) instead of the default string comparison.  
  
 ---- 
 AddToMe