I am writing a script to grab the output from snoop or tcpdump and decode a Cisco Discovery Protocol packet. I want to have snoop/tcpdump killed after so many seconds if I don't get a packet.

I have tried using alarm in an eval block per the example in the alarm doc and it works great. Only problem is that after the script ends I sometimes have zombie processes of tcpdump hanging around if I die in the eval block. Is there a way around this or is the best way to use fork? From the perlipc manpage is seems that the backticks should take care of any zombies without having to call wait(), but I guess not. TIA for the help!
eval { local $SIG{ALRM} = sub { die "tcpdump Died\n" }; alarm 20; @captured_data = `$tcpdump -i $nic -c1 -xx -s0 'ether[20:2] == + 0x2000' 2>/dev/null`; alarm 0; }; # Check the status of eval to see if we alarmed out if ($@) { return; } else { return @captured_data; }; }; After the script runs: grapeape:~# ps -ef |grep tcpdump |grep -v grep root 3499 1 1 16:26 pts/1 00:00:00 sh -c /usr/sbin/tcpdum +p -i eth0 -c1 -xx -s0 'ether[20:2] == 0x2000' 2>/dev/null root 3500 3499 2 16:26 pts/1 00:00:00 /usr/sbin/tcpdump -i e +th0 -c1 -xx -s0 ether[20:2] == 0x2000

In reply to Eval leaving zombies when dying with alarm by yogsothoth

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.