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

Is it possible to let a perl script start another perl script independent of the first?
The first script will be a .cgi that shall start another perl script.

Replies are listed 'Best First'.
Re: Have perl to start another perl script
by Corion (Patriarch) on Jul 25, 2007 at 07:25 UTC

    merlyn wrote Watching long processes through CGI, which allows you to run a process in the background and monitor the progress through a CGI script. If you "just" want to launch another script, the system function is what you want/need.

Re: Have perl to start another perl script
by atemon (Chaplain) on Jul 25, 2007 at 08:06 UTC

    Hi,

    Do it in many ways! put either of the following in your a.cgi, to invoke b.cgi

    1. system("b.cgi");
    2. exec("b.cgi");
    3. `b.cgi`

    Other options available are Threads and fork. These two are intended to have the parent monitor the child easily. Please refer to documentation for details.

    Cheers !

    --VC

    There are three sides to any argument.....
    your side, my side and the right side.

      I would like to "daemonizat" the other perl script and completly dissociate it from the parent script. But save the child pid to a file. I tried the following but the system returns 0 as pid.
      $pid = system("other.pl $IPaddress &"); #my $pidfile = "/home/ember/devices/$Macaddress/$pid.pid"; #open (FILE, "> $pidfile") or die "Can't open file $pidfile\n"; #close FILE;

        You can start the script and obtain the pid using a piped open.

        my $pid = open KID, "| /path/to/your/perl theScript.pl" or die ... print KID $IPAddress; close KID;

        I'm not sure if there are any caveats with this on your platform.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        system always returns 0 on successful launch. I recommend you do all the pid-saving in a wrapper shellscript (or Perlscript) around your child program:

        #!/usr/bin/perl -w use strict; my $pid = $$; my ($IPaddress) = @ARGV; # Save our PID into the pidfile: my $pidfile = "/home/ember/devices/$Macaddress/$pid.pid"; open (FILE, "> $pidfile") or die "Can't create file $pidfile: $!\n"; print FILE "$pidfile\n"; close FILE; # and replace ourselves with the real program, which gets the same PID + as we had: exec 'other.pl', $IPaddress;