trying to implement a script that will produce the resident memory of a PID if provided the port the PID runs on. I am able to produce a script that will provide the resident memory if provided the PID, but if the PID changes i.e a reboot the script no longer works. The script to produce the resident memory from the PID is as follows:
#!/usr/bin/perl
$PID=XXXX;
@stat=split(" ",`ps -o rss $grep $PID`);
$exit=`echo $?`;
if ( $exit == 0 ) {
print "Message: Total amount of memory in kB: $stat[1]\n";
print "Statistic: $stat[1]\n";
exit 0;
}
print "Message: ERROR: Can't find memory field (Mem) in ps -o command. \n";
exit 1;
The above works flawlessly when given the PID.
The PID was calculated by logging on to the box using
ps aux | grep program.jar | grep port=YYYY
Running the above in a script work as follows:
#!/usr/bin/perl
@PID=split(" ",`ps aux | grep activemq.jar | grep port=XXXXX`);
$exit=`echo $?`;
if ( $exit == 0 ) {
print "Message: Total amount of memory in kB: $PID[1]\n";
print "Statistic: $PID[1]\n";
exit 0;
}
print "Message: ERROR: Can't find memory field (Mem) in ps -o command. \n";
exit 1;
When trying to combine the two together as follows
Statistic.1 is the correct PID but Staistic.2 is not the correct for the resident memory for the PID number provide.
#!/usr/bin/perl
@PID=split(" ",`ps aux | grep program.jar | grep port=YYYYY`);
@stat=split(" ",`ps -o rss $grep $PID`);
$exit=`echo $?`;
if ( $exit == 0 ) {
print "Message: Total amount of memory in kB: $stat[1]\n";
print "Statistic.1: $PID[1]\n";
print "Statistic.2: $stat[1]\n";
exit 0;
}
print "Message: ERROR: Can't find memory field (Mem) in ps -o command. \n";
exit 1;
Does not seem to be picking up the correct PID in the ps -o statement.