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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.