简体   繁体   中英

Executing a php script in order

I have a multiple php scripts that are working currently right now. I created a cron job to execute all the scripts at a given time. But now the client want a trigger/event type so he can execute those scripts. So I thought of using the exec function.

So here is the problem, those script has to be executed in order. Eg: I have 2 scripts namely step1.php & step2.php. How do I run the 2 php script in order and in the backgroud process.

I read that using the 3rd parameter in exec function can return a result but it only always gave me a result: string(0) ""

This is what I want to achieve:
$step1 =  exec("php step1.php > /dev/null &", $output, $returnVal);
if($step1 === TRUE) exec("php step2.php > /dev/null &", $output, $returnVal);

Or maybe their another php function that are more suitable than using exec??? Really don't know. Please help

Thanks a many guys

Assuming that you have step1.php outputting true on success

$step1 =  exec("php step1.php > /dev/null &", $output, $returnVal);

$step1 is now the last line from the result of the command.

[EDIT: Misread the manual. Depending on what you want, you probably do need to check $step1 for the outputted result, and you are getting an empty string because you're not outputting anything in the step1.php script? Everything else appears to be correct however.]

What you should be checking is $returnVal for return status. This variable is passed by reference (according to the manual), so your code should be:

exec("php step1.php > /dev/null &", $output, $returnVal);
if($returnVal === TRUE) exec("php step2.php > /dev/null &", $output, $returnVal);
if($returnVal === TRUE) exec("php step3.php > /dev/null &", $output, $returnVal);

You could even use a while loop:

$num_steps = 4; //Arbitrary number for example
$step = 1;

while($returnVal === TRUE && $i <= $num_steps) {

    exec('php step'.$step.'.php > /dev/null &', $output, $returnVal);
    $i++;
}

[Careful, I haven't tested the above and it may not be suitable for what you want to do]

EDIT: islanddave's answer below is 'better'. I assumed that your current process was set, and you cannot change it (eg amount of time you have, can't refactor existing code for legacy reasons etc.).

You don't have to run exec() from a php script to handle code in other php scripts. There are probably a dozen ways to do what you want, but one I would probably use:

Functionalize the code in step1.php and step2.php:

Old (Pseudo):

<?php
    $var = true;
    return $var;
?>

New:

<?php
    function foo() {
        $var = true;
        return $var;
    }
?>

Include those scripts (because the code is now functionalized, it doesn't get executed until you call the functions). So, in your script that calls the step scripts:

<?php 
    include('step1.php');
    include('step2.php');  
?>

Now call the functions you need with whatever logic you require:

<?php
    include('step1.php');
    include('step2.php');

    if(foo() == true) {
        bar(); //bar() is found in step2.php
    }
?>

Again, several ways to accomplish this, much depending on your requirements and what the code in the step php scrips are doing. I'm making assumptions about that given the lack of details about what step1 and step2 are trying to execute.

Call the second script on the end of the first one.

if (... == TRUE) {
  include('second_script.php');
}

And then you only need to run the first script on cron.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM