I've used this script (refined many times) with success. I don't use it for time, but checking the man for 'ps' you can get that info also. I use it from the parent to check on possible runaway child processes. If the ( current size - original size ) > max size, then I kill the child and fork a new child. (Note: the child may actually be working correctly, so the 1st kill is to ask the child to take an early quit, and if that doesn't work, I do a system kill.)
You need to test how the kill is handled on different operating systems, but in general it's fast and works. I've used this on AIX, Linux ( Debian, openSuse, Ubuntu, and probability others ). You don't need the $Debug test, but I left it in to give you some ideas on debugging help. The 'Die_Rtn' can be replaced with a 'die', but I do "die" differently depending on whether it's needs a terminal or web response. You could also put the open in an 'eval'.
Consider it a start, since the parent needs to know what operating system to consider for the max size. There are large differences between the operating systems, and also 32bit and 64bit environments.
# How it's called my $NAME = "myperlpgm.plx"; my ($pmem1,$pmem2) = Display_Mem_Usage($$,$NAME); sub Display_Mem_Usage { # VSZ is size in KBytes of the virtual memory ( VSZ * 1024 ) # RSS is size in pages of real memory ( 1024 * RSS ) my $cpid = shift; my $name = shift; my $var = ""; my $fh; if ( $Debug >= 3 ) { $0 = "$name: Checking. . . "; } my $arg = qq| -o "vsz rssize" -p $cpid|; open ( $fh, "-|", "$PS $arg" ) or Die_RTN("Prefork: Not open \'$PS +\': $!"); while (<$fh>) { $var .= $_; } close $fh; my $rno = my @ref = split(/\n/,$var); if ( $rno < 2 ) { return ( -1, -1 ); } my $info = join(" ", split " ", $ref[1]); my ($vmem,$rmem) = ( split(/\ /,$info) ); ## { syslog('info',"Display_Mem_Usage: $PS $arg|$vmem|$rmem|$va +r"); } if ( $Debug >= 3 ) { syslog('info',"Mem_Usage: |$vmem"."K,$rmem"."K|$var"); } return ( $vmem * 1024, $rmem * 1024 ); }
Hope it helps!
"Well done is better than well said." - Benjamin Franklin
In reply to Re: My Program Stats
by flexvault
in thread My Program Stats
by gulden
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |