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
...
...
?> 
user3188554
  • 67
  • 1
  • 6
  • 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.) – Harry Johnston Nov 12 '14 at 00:54

2 Answers2

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

Community
  • 1
  • 1
iLot
  • 98
  • 1
  • 7
  • 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 Nov 12 '14 at 14:19
  • @user3188554 The command can be used to start another script. Look at the ["Command Line PHP on Microsoft Windows"](http://php.net/manual/en/install.windows.commandline.php) page of php.net to find out how to do it on your machine. – iLot Nov 12 '14 at 14:36
  • 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 Nov 13 '14 at 14:28
  • @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](http://de2.php.net/manual/en/function.exec.php#43917) (I would update my answer accordingly if it works). – iLot Nov 13 '14 at 19:51
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.

Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
  • 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. – Harry Johnston Nov 12 '14 at 00:58
  • 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. – user3188554 Nov 12 '14 at 14:21