http://www.perlmonks.org?node_id=192421

Since I don't have a working fork, this code will spawn a process in the background on win32. It is specifically setup to run one of my perl scripts with a few different config files and without a dos window. I drop this in my startup folder and it kicks them off with a 2 second delay between them (the spawned script monitors databases, I don't want to pound the machine with 5 connections all at once...)

#!/usr/bin/perl use strict; use warnings; use Win32::Process; use Win32; my $sleep = shift || 2; my $pl = "traydb.pl"; my $pldir = "C:\\perl\\code\\TrayDb\\"; my @plargs = ('-configfile bdprod.cfg', '-configfile fdprod.cfg', '-configfile vdprod.cfg', '-configfile fddvlp.cfg', '-configfile bddvlp.cfg'); sub ErrorReport{ print Win32::FormatMessage( Win32::GetLastError() ); } sub SpawnTrayDb { my $ProcessObj = shift; my $pl = shift; my $plargs = shift; Win32::Process::Create($ProcessObj, "c:\\perl\\bin\\perl.exe", "perl $pl $plargs", 0, DETACHED_PROCESS, $pldir)|| die ErrorReport(); } foreach (@plargs) { print "$pl $_\n"; sleep $sleep; SpawnTrayDb(my $po, $pl, $_); }

Replies are listed 'Best First'.
Re: They didn't give me a fork so I have to eat with a spawn.
by bart (Canon) on Aug 23, 2002 at 20:52 UTC
    Win32's Perl 5.6.1 does have an emulated fork(), though. Is there something wrong with that?
      Win32's Perl 5.6.1 does have an emulated fork(), though. Is there something wrong with that?

      Yup.

      If you try to fork and "spawn" off a bunch of system() calls, you get "Resource temporarily unavailable" after some fairly small number of forks (maybe 20?).

      With Win32::Process::CreateProcess, you don't run into that problem.
      --
      Mike

        Good call.

        BTW you should look into using $^X instead of the hardcoded path to perl.

        will the parent process have to wait until the child complete?
      I've never had much luck with ActiveState's fork. I don't even consider using it when I'm on win32. Most of my problems have been related to sockets. When a daemon accepts a connection and forks off, the socket doesn't have anything to say until the process dies. A socket that doesn't pass data isn't much use to me.

      So I just keep the servers on the servers on client stuff like this on my workstation.

      This code is more usefull for launching perl code without a dos window though, and that's why I wrote it. With this you don't have to bother hiding your dos parent in your code, it's already gone.