#!/usr/bin/perl # our needed packages use strict "vars"; use Getopt::Std; # Global Variables my $VERBOSE = 0; my $DEBUG = 0; my $OUTPUT; # commandline options my $opt_string = "vdhc:i:o"; getopts( "$opt_string", \my %opt ) or usage() and exit(1); $VERBOSE = 1 if $opt{'v'}; $DEBUG = 1 if $opt{'d'}; $OUTPUT = 1 if $opt{'o'}; if ( $opt{'h'} ) { usage(); exit 0; } my $COMMAND = $opt{'c'}; my $INTERVAL = $opt{'i'}; ( usage() and die "You have to specify a command!! \n") unless $COMMAND; if ( not $INTERVAL ) { $INTERVAL = 3; } my $USER = qx("whoami"); chomp $USER; #################################################### # Main part my $PID; my $PSTATUS; my $MINORPF; my $MAJORPF; my $CPUTIME; my $THREADS; my $VMSIZE; system("$COMMAND&"); open(PSAUX,"ps aux |") or die "Error creating PSAUX file $!\n"; while ( my $targetline = ) { if ($targetline =~ /^$USER\s+(\d+)\s.*\d:\d\d\s$COMMAND$/) { # print "PS AUX RELATED LINE IS: $targetline\n "; $PID = $1; print "\n"; print "Process ID is: $PID , Refresh Interval is: $INTERVAL \n"; print "-------------------------------------------------------------------------\n" } } close (PSAUX); if ($OUTPUT) { open (OUTFILE, ">$PID") or die "Error creating OUTPUT file $!\n"; print "Writing the results to the file $PID Please wait...\n"; } while (stat ("/proc/$PID") ) { my $TIME = time; if ($PID) { open(STAT, "/proc/$PID/stat") or die "Error creating STAT file $!\n"; my $line = ; close(STAT); my @statarray = split /\s+/,$line; # print @statarray[1]; $PSTATUS = @statarray[2]; $MINORPF = @statarray[9]; $MAJORPF = @statarray[11]; $CPUTIME = @statarray[15]; $THREADS = @statarray[19]; open(STATUS,"/proc/$PID/status") or die "Error creating STATUS file $!\n"; while ( my $targetline = ) { if ($targetline =~ /^VmSize:\s+(\d+)\s+kB $/) { $VMSIZE = $1; } } close (STATUS); if ($OUTPUT) { print OUTFILE "TIMESTAMP:$TIME,PSTATUS:$PSTATUS,MINORPF:$MINORPF,MAJORPF:$MAJORPF,CPUTIME:$CPUTIME,TREADS:$THREADS \n\n"; } else { print "TIMESTAMP:$TIME,PSTATUS:$PSTATUS,MINORPF:$MINORPF,MAJORPF:$MAJORPF,CPUTIME:$CPUTIME,TREADS:$THREADS \n\n"; } sleep $INTERVAL; } } close(OUTFILE); # system ("kill -9 $PID"); exit 0; ##################################################### # subroutines sub usage { print "Usage:\n"; print "-h for help\n"; print "-v for verbose (more output ) \n"; print "-d for debug (even more output)\n"; print "-c The linux command to run and track it \n"; print "-i