note 110131 added to function.exec

From: Date: Sat, 22 Sep 2012 20:52:22 +0000
Subject: note 110131 added to function.exec
Groups: php.notes 
Request: Send a blank email to php-notes+get-190962@lists.php.net to get a copy of this message
To run your command in the background on Windows, you have to use start /B before your command (in Linux add & after your command). exec() simply uses cmd /C. The /C carries out the command specified by the string and then terminates. However start /B needs to be put before that, so you cannot use exec() for running a command in the background on Windows. Instead use popen. Example: pclose(popen('start /B php yourscript.php > result.txt 2> error.log', 'r')); works. But what if you have spaces in your filenames? Escape them with double quotes: pclose(popen('start /B php "your script.php" > "my result.txt" 2> "error.log"' , 'r')); works. But now let's assume that you do not have php in your path. This does not work: pclose(popen('start /B "C:\Program Files (x86)\PHP\php.exe" "your script.php" > "my result.txt" 2> "error.log"' , 'r')); Why? Because cmd only preserves the double quotes if there is not one of: &<>()@^| special characters between the two double-quote characters. Double quotes between double quotes are preserved however if /S is used, in that case only the two outer double quotes are removed. So add two double quotes surrounding your command and add the default cmd /C plus the extra /S like: pclose(popen('start /B cmd /S /C ""C:\Program Files (x86)\PHP\php.exe" "your script.php" > "my result.txt" 2> "error.log""' , 'r')); ---- Server IP: 69.147.83.199 Probable Submitter: 77.251.5.31 ---- Manual Page -- http://www.php.net/manual/en/function.exec.php Edit -- https://master.php.net/note/edit/110131 Del: integrated -- https://master.php.net/note/delete/110131/integrated Del: useless -- https://master.php.net/note/delete/110131/useless Del: bad code -- https://master.php.net/note/delete/110131/bad+code Del: spam -- https://master.php.net/note/delete/110131/spam Del: non-english -- https://master.php.net/note/delete/110131/non-english Del: in docs -- https://master.php.net/note/delete/110131/in+docs Del: other reasons-- https://master.php.net/note/delete/110131 Reject -- https://master.php.net/note/reject/110131 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#190962) next »