1

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);

?>
8
  • You don't need a while loop for pcntl_waitpid. What exactly is your question? Commented Jun 23, 2016 at 19:00
  • How to increase the time limit until execution of perl script is finished inside php script while using it in localhost. Commented Jun 23, 2016 at 19:05
  • There shouldn't be a time limit if you are executing it in CLI. How are you executing it and what error are you receiving? Commented Jun 23, 2016 at 19:20
  • Thanks for replying. So in browser I am running by using localhost/hello.php. Normally in command line my perl script execution is finished in 10 min . But here its getting finished only in 1 min without completion and without error. Commented Jun 23, 2016 at 19:24
  • 1
    @Borodin, that makes no sense. An acronym is a legitimate shortening of a phrase, lyk is just a misspelled word. It'd be like using USA instead of United States... please troll elsewhere. Commented Jun 23, 2016 at 22:18

1 Answer 1

1

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  ");
?>
Sign up to request clarification or add additional context in comments.

6 Comments

You can remove the trailing spaces, too (and I'm skeptical whether -f is needed).
Well spotted, my apologies for copypasting the line from the original post!
As Already I did tried your simple suggestion. But it did't work. That is why I switched on to forking.
What didn't work about it? If an exec call by itself doesn't work, I don't see how the same exec call inside a forked child process would work either. Have you tried using absolute paths to both perl and script.pl?
So in both cases (forking and without forking) the php script is working, but the execution of perl script is getting finished in 1 min while using it on localhost but in command line perl script its taking 10 min to complete the whole process . So I need to increase time limit for the localhost.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.