Sorry to revive this, but I have a problem with the way my code is working and it is related to this issue.

I am using the same if...fork...else...exec process as @davorg but when I look at the ps -ef output I find 2 processes.

if(my $pid = fork()){ $this->set_pid_in_db($id, $pid); } # Child process starts service and inherits child PID else{ my $command = sprintf("%s %s 1>>%s 2>>%s", $this->fnClient(), $id, + $this->fnOe($id), $this->fnOe($id)); #my $command = sprintf("%s %s", $this->fnClient(), $id); exec($command); } >ps -ef | grep SB mark 11970 1 0 07:57 pts/1 00:00:00 sh -c /code/SBClient2.pl j +ob1 1>>/log/log.1.oe 2>>/log/log.1.oe mark 11971 11970 77 07:57 pts/1 00:00:06 /usr/bin/perl /code/SBClie +nt2.pl job1

And the PID that was set on line 2 is 11970 rather than 11971. Of course, I can still test to see if 11970 is running and that will tell me if 11971 is running. And I can kill 11970 and that will kill 11971.

However, what confuses me is that if I don't redirect output and error to a file (i.e. use the commented out command instead), then 1 process rather than 2 processes are started.

Does anyone know why this happens?


In reply to Re: getting a child's process ID by markdibley
in thread getting a child's process ID by Seshouan

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.