<p>I've had some trouble forking of processes from a Perl CGI script when running on Windows. The main issue seems to be that 'fork' is emulated when running on windows, and doesn't actually seem to create a new process (just another thread in the current one). This means that web servers (like IIS) which are waiting for the process to finish continue waiting until the 'background' process finishes.</p>

<p>Is there a way of forking off a background process from a CGI script under Windows? Even better, is there a single function I can call which will do this in a cross platform way?</p>

<p>(And just to make life extra difficult, I'd really like a good way to redirect the forked processes output to a file at the same time).</p>

[{'answer_id': 45801, 'author': 'Espo', 'author_id': 2257, 'author_profile': 'https://Stackoverflow.com/users/2257', 'pm_score': 0, 'selected': False, 'text': '<p><a href="http://www.xav.com/perl/lib/Pod/perlfork.html" rel="nofollow noreferrer">perlfork</a>:</p>\n\n<blockquote>\n  <p>Perl provides a fork() keyword that\n  corresponds to the Unix system call of\n  the same name. On most Unix-like\n  platforms where the fork() system call\n  is available, Perl\'s fork() simply\n  calls it.</p>\n  \n  <p>On some platforms such as Windows\n  where the fork() system call is not\n  available, Perl can be built to\n  emulate fork() at the interpreter\n  level. While the emulation is designed\n  to be as compatible as possible with\n  the real fork() at the the level of\n  the Perl program, there are certain\n  important differences that stem from\n  the fact that all the pseudo child\n  ``processes\'\' created this way live in\n  the same real process as far as the\n  operating system is concerned.</p>\n</blockquote>\n'}, {'answer_id': 45815, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 2, 'selected': False, 'text': '<p>Use <a href="http://search.cpan.org/~jdb/Win32-Process-0.14/Process.pm" rel="nofollow noreferrer">Win32::Process->Create</a> with DETACHED_PROCESS parameter</p>\n'}, {'answer_id': 45836, 'author': 'Leon Timmermans', 'author_id': 4727, 'author_profile': 'https://Stackoverflow.com/users/4727', 'pm_score': 4, 'selected': True, 'text': '<p>If you want to do this in a platform independent way, <a href="http://search.cpan.org/~bzajac/Proc-Background/lib/Proc/Background.pm" rel="nofollow noreferrer">Proc::Background</a> is probably the best way.</p>\n'}, {'answer_id': 239477, 'author': 'Spedge', 'author_id': 31161, 'author_profile': 'https://Stackoverflow.com/users/31161', 'pm_score': 0, 'selected': False, 'text': "<p>I've found real problems with fork() on Windows, especially when dealing with Win32 Objects in Perl. Thus, if it's going to be Windows specific, I'd really recommend you look at the Thread library within Perl.</p>\n\n<p>I use this to good effect accepting more than one connection at a time on websites using IIS, and then using even more threads to execute different scripts all at once.</p>\n"}, {'answer_id': 49216535, 'author': 'Jeffrey Tackett', 'author_id': 6789640, 'author_profile': 'https://Stackoverflow.com/users/6789640', 'pm_score': 0, 'selected': False, 'text': '<p>This question is very old, and the accepted answer is correct.  However, I just got this to work, and figured I\'d add some more detail about how to accomplish it for anyone who needs it.</p>\n\n<p>The following code exists in a very large perl CGI script.  This particular sub routine creates tickets in multiple ticketing systems, then uses the returned ticket numbers to make an automated call via Twilio services.  The call takes awhile, and I didn\'t want the CGI users to have to wait until the call ended to see the output from their request.  To that end, I did the following:</p>\n\n<pre><code>  (All the CGI code that is standard stuff.  Calls the subroutine needed, and then)\n\n  my $randnum = int(rand(100000));\n  my $callcmd = $progdir_path . "/aoff-caller.pl --uniqueid $uuid --region $region --ticketid $ticketid";\n  my $daemon = Proc::Daemon-&gt;new(\n    work_dir     =&gt; $progdir_path,\n    child_STDOUT =&gt; $tmpdir_path . \'/stdout.txt\',\n    child_STDERR =&gt; $tmpdir_path . \'/stderr.txt\',\n    pid_file     =&gt; $tmpdir_path . \'/\' . $randnum . \'-pid.txt\',\n    exec_command =&gt; $callcmd,\n  );\n  my $pid = $daemon-&gt;Init();\n\n  exit 0;\n\n  (kill CGI at the appropriate place)\n</code></pre>\n\n<p>I am sure that the random number generated and attached to the pid is overkill, but I have no interest in creating issues that are extremely easily avoided.  Hopefully this helps someone looking to do the same sort of thing.  Remember to add <code>use Proc::Daemon</code> at the top of your script, mirror the code and alter to the paths and names of your program, and you should be good to go.  </p>\n'}]
['https://Stackoverflow.com/questions/45792', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/797/']