I'm pretty much a newbie to Perl and am trying to write a script that forks. The child will use Net::RawIP and opens a packet capture descriptor and reads an unspecified number of packets. After 10 seconds, the parent, signals the child to terminate using a KILL signal. The problem is that a <defunct> process for the child remains, at least as far as ps -eaf is concerned. After one minute, the script restarts again. I need to be able to kill the child process after a specified length of time. Any suggestions on the best way to do this?

#!/usr/bin/perl use warnings; use strict; use Net::RawIP; use POSIX; daemonize(); while (1) { my $pid = fork(); print "Process number of script is $$\n"; unless (defined ($pid)) { die "Resources unavailable to fork\n"; } elsif ($pid == 0) { my $a = new Net::RawIP; my $p = $a->pcapinit("eth0","udp port 68",1500,30); my $f = ["User-defined message"]; loop $p,10,\&process_pkt,$f; } elsif ($pid) { for (my $i=0; $i <= 10; $i++ ) { sleep 1; print "in parent...$i seconds have elapsed...\n"; } print "killing child process $pid"; kill 9, $pid; sleep 60; } } # end of while loop sub process_pkt { print "packet captured\n"; my $raw_pkt = $_[2]; my $raw_data = unpack "H*", $raw_pkt; print $raw_data,"\n"; } sub daemonize { my $pid = fork(); exit if $pid; setsid(); }

In reply to Best way to kill a child process by doylebobs

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.