Have each child write its status to a pid-named file in a status directory. When its work is complete, have it delete its file. If you use File::Temp, the cleanup is automatic.
use File::Temp; use constant STATUS_DIR => '/tmp/status-dir'; { my $fh; # hidden from outer code # status is logged only when you have created # the status dir by hand; this lets you turn off # logging by deleting the status dir if ( -d STATUS_DIR ) { $fh = new File::Temp( template => "$$-XXXXXX", dir => STATUS_DIR, suffix => ".txt", unlink => 1 ) or die "can't open temp file: $!"; $fh->autoflush(1); } sub log_status { print $fh "$$ - ", scalar localtime, " - ", @_, "\n" if $fh; } sub close_status { $fh->close if $fh; } }
Then your work code can log its progress:
# here is the meat of our worker code log_status("worker starting; config=blah..."); # ... work ... log_status("worker starting stage two"); # ... more work ... log_status("worker starting stage three"); sleep 30; # it's a long one # finally! whew, that was hard work :) log_status("worker exiting"); close_status();
If a worker bee gets hung up, you can observe its status from the log that corresponds to its pid. For example, I justed fired one up with pid 14416. Let's see what it is doing:
$ cat /tmp/status-dir/14416-UXL0uq.txt 14416 - Fri Nov 12 12:40:06 2004 - worker starting; config=blah... 14416 - Fri Nov 12 12:40:06 2004 - worker starting stage two 14416 - Fri Nov 12 12:40:06 2004 - worker starting stage three
Ah, it's that darn stage-three work! It always takes too long.

Also, since the log files are automatically deleted upon completion, you can see what tasks are still running by listing the contents of the status-log directory.

Cheers,
Tom


In reply to Re: Suggestions on differentiating child processes by tmoertel
in thread Suggestions on differentiating child processes by gnu@perl

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.