in reply to Problems with flushing after fork()

You'll have to:

close STDOUT; close STDIN; close STDERR;
not just STDOUT in order to let it work

Replies are listed 'Best First'.
Re^2: Problems with flushing after fork()
by smiffy (Pilgrim) on Feb 12, 2010 at 03:30 UTC

    Thanks. Whilst this lets the desired output return immediately, this is all that happens - the sleep() is no longer executed. No process exists after the output comes back.

      To complete the separation you must also dis-associate the forked child from the parent.
      use POSIX qw(setsid); ... # inside your forked child setsid() or die "Can't start a new session: $!";

        Thanks - I'll give that a try on Monday. Trying to have a couple of days sanity break now ;-)

      Well, I've just tried it now and it works

      #!/usr/bin/perl use strict; use warnings; use CGI; my $q = CGI->new; print $q->header; if (my $pid = fork ) { print "Hello, world - PID $pid\n"; exit; }else{ close STDOUT; close STDERR; close STDIN; sleep(20); exit; }

      I have verified that the script is still running: ps auxx | grep perl

        Did you test this running as a CGI process or from the command line? It works fine for me from the command line but not when run as a CGI. Guess that Apache's adding complications.