简体   繁体   中英

php exec in the background with WAMP on Windows

with the following code i can call a php script and pass some variables into it

$cmd = 'php -f C:/wamp/www/np/myphpscript.php '.$var1;


exec($cmd); 

this way my called script works, but , i need that process to be in the background , i dont want to wait for the script to finish, is there any way of doing that using wamp on windows ? been doing some reading and some add a & at the end of the command, or a > NUL , now i noticed some of them are for linux , is there such a command for wamp on windows ? if there is please share it

Don't know what you are running and if you get a response to your command. But maybe it helps if you open a tab for each command. So you can see responses of each running script and at the end you can call javascript to close the tab.

EDIT : Due to the way the exec() command waits for the program to finish executing, it's very difficult to do this with vanilla exec() . I came across these solutions , and this one should work:

$rshell = new COM("WScript.Shell");
$rexec = $rshell->Run("php -f C:/wamp/www/np/myphpscript.php ".$var1, 0, false);

The WScript.Shell->Run command takes 3 arguments: the command (you can optionally add output redirection), window mode (0 = hidden), and wait it should wait to finish. Because the 3rd argument is false, this PHP should return immediately.

Original Solution : As this post suggests, you should try START /B cmd . It is virtually the Linux equivalent of cmd & in that it runs the command asynchronously, in the background, without user interaction or opening a new shell.

Because this will return immediately, PHP won't wait for it to finish, and the exec() command will not receive any output. Instead, try using shell output redirection. Your PHP given code would look like this:

$cmd = 'start /b "" php -f C:/wamp/www/np/myphpscript.php '.$var1.' >C:/wamp/www/np/output.txt';
exec($cmd); 

You must set the variable php on windows environment !

If you have already done so skip the tutorials steps:

 1. Open:
        My Computer => Properties => Change Settings

 2. Select the tab: Advanced

 3. Click Environment Variables: Variable system

 4. Click the button New
        Add the name of the environment variable. Example = php
        Add the path to executable php.exe. Example = D:\xampp\php\php.exe

Create a file myscript.php

The variariaveis $argc and $argv are native php.

You will notice that $ argc always carries the same value as the result of calling count ( $argv ) in any case $argc is the standard used and is a few milliseconds faster by being in memory (if that makes any difference in performance your script).

//\n skip line
echo "\n\n";
//echo test debug
echo "Print Total Args : ";
//Print return variavel $argc
print_r($argc);
//\n skip line
echo "\n\n";
//echo test debug
echo "Print Array Args : \n\n";
//Print return variavel $argv
print_r($argv);
echo "\n";
// You can retrieve the arguments in the normal way.
$myvar_count = $argc;
$myvar_array_args = $argv;

Or if you want to set is not the environment variable, simply can call the path

Example: D:\xampp\php\php.exe myscript.php argument1 2 3 4 5

Retorn the Prompt in Windows

Total Args : 5

Array Args :

Array
(
    [0] => test.php
    [1] => argumento1
    [2] => 2
    [3] => 3
    [4] => 4
)

I hope this helps! See you later!

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