You've already been given better solutions for your task, but it's still interesting to understand why your approach didn't work... so

2. Why does process 29309 still show when I have killed it in my code?

You're using a negative signal (intentionally or not), which means you're sending the signal to a process group (29309 in your case). This is generally a reasonable approach here (because it would have the cat process be signaled as well, which would otherwise remain running, if you kill the shell only). It didn't work, however, as a process group with that ID didn't exist.

When doing a ps (with customized output options (Linux syntax)) while all processes were still running, you'd have gotten something like:

$ ps axf -o pid,ppid,pgrp,cmd PID PPID PGRP CMD ... 29307 4061 29307 \_ /usr/bin/perl ./661639.pl 29309 29307 29307 \_ sh -c cat /dev/urandom > testfile 29310 29309 29307 \_ cat /dev/urandom

which shows that the process group is not 29309, but rather 29307, because the shell in this case doesn't create a new process group.

In other words, one of the following would have worked (note that you don't need signal 9 here, 15 (SIGTERM) is absolutely sufficient):

kill -15 => getpgrp($pid); # process group of child kill -15 => getpgrp; # process group of current process (the sc +ript) # or even (as $$ equals the process group in this case): kill -15 => $$;

( A problem could be (depending on context) that the script does get killed, too... but I won't try to come up with a workaround for that, because it's moot by now, anyway. )


In reply to Re: exec creates two processes.. how to kill them? by almut
in thread exec creates two processes.. how to kill them? by santosh_sugur

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.