2

I have been searching through several questions similar to this but i have found no answer suitable for me, especially for Windows system. I have installed PHP and Apache ona Windows machine. I have an app which calls Page1.php. In Page1 i execute some code and at an specific point I would like to launchanother php script, let`s call it Script.php asynchronously. I don´t need to pass any parameter to this second script -if I could it could be helpful but it´s not mandatory- and I don´t need to wait for the script to finish beacuse i don´t want to retain the communication with the user, since script makes a lot of operations with files and it can last quite a long time. I have seen and tried answers based on exec, popen, shell, system and so on...but I haven´t found the right one for me, maybe because I´m working on Windows.These have beeen some of the solutions I have tried without anysuccess:

exec('php script.php &> /dev/null &');
shell_exec('php script.php &> /dev/null &');
system('php script.php &> /dev/null &');
php script.php &> /dev/null &`

The schema could be more or less as follows:

PAGE1.PHP

<?php
echo "Hello World";
echo "\n";
....
if ($var>$var2) {
    HERE CALL SCRIPT.PHP ASYNC
}
...
?> 

SCRIPT.php

<?php
...
...
?> 
1
  • Did you try that command at the command line? It produces a syntax error, because &> doesn't mean anything in Windows. (Neither does /dev/null.) Commented Nov 12, 2014 at 0:54

2 Answers 2

2

Copied from php.net's comments on exec's page:

This will execute $cmd in the background (no cmd window) without PHP waiting for it to finish, on both Windows and Unix.

<?php 
function execInBackground($cmd) { 
    if (substr(php_uname(), 0, 7) == "Windows"){ 
        pclose(popen("start /B ". $cmd, "r"));  
    } 
    else { 
        exec($cmd . " > /dev/null &");   
    } 
} 
?>

I did not test this myself, but it was a similar direction to what I was thinking of. You can also look at this stackoverflow question for more details (might help).

Sign up to request clarification or add additional context in comments.

4 Comments

But in this approach how I execute Script.php. As i can see from your code it only executes command line, is that right?
@user3188554 The command can be used to start another script. Look at the "Command Line PHP on Microsoft Windows" page of php.net to find out how to do it on your machine.
OK, I almost got it but it is not still working, I guess the problem is the account permissions. Maybe the account needs to have admin privileges, does that make sense?
@user3188554 it does. You can try running your script using the same command you use here and see if it works. If it doesn't- try running cmd as administrator and try again- if it works, you should make sure you always execute this as an administrator. If that is not the issue, you can try using WScript.Shell as written in this php.net comment (I would update my answer accordingly if it works).
0

What you call "asynchronous" is not actually asynchronous. You make a call for another script to execute in case $var > $var2 and it's called an if-statement.

If your script has to be in another file, you may try using include, include_once or require or require_once:

if ($var > $var2) {
    include "Script.php";
}

The rest depends on Script.php.

2 Comments

The OP wants to know how to call another script asynchronously. The fact that he wants this to happen inside an if-statement is just an irrelevant detail.
Hi Harry, that is the point. The if statement is completely irrelevant. I need to execute script.php without Page1.php being waiting for it to finish to continue its own line of execution.

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.