mikeo has asked for the wisdom of the Perl Monks concerning the following question:

Note: I am a Perl newbie with about 10 days of experience in the language. Translation: "I'm dumb. Don't make me cry too much."

I'm presently writing a command line interface for a script. I want to be able to start and stop a second process from the first. I have been unable to figure out a solution that works for my specific situation. Here's what I've tried so far:

Option#1:

system("start ANYFILE.pl");

This allows me to fire off the second Perl script in the background, but I am unable to figure out a way to maintain some kind of reference to the process through which I can kill it.

Option #2:

$PID = fork();

This would allow me to keep a handle on the process if I were able to figure out how to use it to fire off the second Perl script.

Option #3:
Win32::Process::Create( my $ProcessObj, "c:\\dir1\\dir2\\ANYFILE.pl", 0, 0, NORMAL_PRIORITY_CLASS, ".") || die "Create process $!";
This would be a great solution, since it allows me to keep a handle on the process. The problem is I am unable to figure out how to launch a Perl script using it. Apparently, even though I have my extensions correct I cannot "execute" a Perl script.

I would appreciate help getting any of these solutions to work, a solution other than these which would solve my problem, and even explainations as to why one or more of these solutions wouldn't work. It's always a plus to learn what you did wrong and why it's wrong!

-Mikeo

BTW, the script is being developed on a Windows box for use on Windows boxes.

Replies are listed 'Best First'.
Re: Running a Perl script in the background from another Perl script
by the_slycer (Chaplain) on Jul 10, 2001 at 22:39 UTC
    Here is how you do it with Win32::Process:
    use Win32::Process; Win32::Process::Create($Win32::Process::Create::ProcessObj, 'c:/perl/bin/perl.exe', #path to perl 'perl script.pl', #perl script 0, DETACHED_PROCESS, ".")or die "$!";
      I would try to avoid hard coding the path to perl, using $^X to keep track of the perl binary.

      perldoc perlvar for more info.

      /J

      Thanks for the quick reply! It works great. -Mikeo
Re: Running a Perl script in the background from another Perl script
by HamNRye (Monk) on Jul 10, 2001 at 22:48 UTC

    As far as option #3 is concerned, you need to exec the perl interpreter with the script as an argument. "C:\perl\perl.exe C:\my\frikkin\script.pl"

    Option 2 is a dead end under windows.

    I'm assuming the problem with #1 is the same as #3

    NT or 2000 will allow you to set up your scripts as services that can be started and stopped from the command line. Use the Srvany.exe. Note, services can interact with users -or- the network, not both. If you need any more information on this, I have a procedure typed up.

    ~Hammy

      I read that you can create a service on an NT machine with Win32:Daemon. I have not tested this yet, but i would love to try it out when time permits. I believe Dave Roth Created this module. Just FYI to my fellow monks. ~Ray~
      Option #2 works fine under Cygwin, which depending upon your situation may not be a limitation.