Kabhir
Kabhir

Reputation: 11

Wait until execution of Perl script finished inside php script

I am trying to execute perl script inside php script. Although it is running but only for short duration. How to increase the time-limit so that the execution of perl script is finished until the final php script is called of ?

I am running php script on the localhost.

Here is the code :

<?php

$maxChildren = 1;  
$pids = array();
$pid = pcntl_fork();

if ($pid) { // Parent

    if ($pid < 0) {
        // Unable to fork process, handle error here
        continue;
    } else {
        $pids[$pid] = $pid;
    }

} else {

    exec("perl -f script.pl  ");
    exit(0);

}

while(pcntl_waitpid(0, $status) != -1);

?>

Upvotes: 1

Views: 456

Answers (1)

Keiji
Keiji

Reputation: 1042

Looks like you've over-complicated the problem. Unless I'm missing something, your entire code can be replaced by a single line

<?php
exec("perl -f script.pl  ");
?>

as this will have the same effect as forking and then immediately waiting for the child process to finish.

To avoid the page timing out before the perl script is finished, simply add set_time_limit(0) before calling exec:

<?php
set_time_limit(0);
exec("perl -f script.pl  ");
?>

Upvotes: 1

Related Questions