It works but my system calls creates 2 zombie process when I kill my main program.

If you just want a quick fix, try putting
$SIG{CHLD}='IGNORE';
in your main code. You may also need to put it in your forked children before you run the exec or system.

If you want a cleaner solution, setup your script to get the $pids.

You can use a different form rather than system or exec to get your pid. Also, there is the problem of getting the pid of the shell, which runs your command thru system or exec. You may need to use Proc::KillFam on the pid to get all it's children. See Best way to kill a child process

#!/usr/bin/perl #When a program forks the fork returns the pid of the #child so the parent can use this to kill it at will. my $pid = fork(); print "pid $pid created\n"; if ( $pid == 0 ) { # child process so do stuff here # usually have an exit to ensure child # does not escape this if clause exit; } else { # parent process, waits a while sleep 3; # kills child if( kill 9, $pid ){ print "pid $pid killed\n"}; } # parent continues on here (as will child if not killed or exited)

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

In reply to Re^5: The proper way to execute echo aabbcc >> file in backround by zentara
in thread The proper way to execute echo aabbcc >> file in backround by young_monk_love_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.