I have tried NUMEROUS methods to start a background php script from another php script. I need to create a zip file of database files for download. I have it set up to download instantly for single files, but if the files need to be zipped, I want a background process to start that will email a link to the zip file. The process works but the original script still waits for the process to finish. I have tried the following methods, and like I said, they work, but they will not go to the background. It had me waiting for over eight minutes for certain instances(large file zips), and no user will wait for that.
print `php -f zipfiles7.php | at now`;
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("C:\wamp\bin\php\php5.4.3\php.exe -f C:/wamp/www/zipfiles7.php", 0, false);
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("cmd /c C:\wamp\bin\php\php5.4.3\php.exe -f C:/wamp/www/zipfiles7.php", 3, false);
$exe = "C:\wamp\bin\php\php5.4.3\php.exe";
$args = "C:\wamp\www\zipfiles7.php";
pclose(popen("start \"bla\" \"" . $exe . "\" " . escapeshellarg($args), "r"));
$cmd = "php-win.exe zipfiles7.php";
$cmd = "start /B php-win.exe zipfiles7.php";
$PID = shell_exec("nohup $cmd > test.txt 2>&1");
exec("Psexec.exe -i -d php.exe zipfiles7.php");
$cmd = "C:\wamp\bin\php\php5.4.3\php-win.exe -f C:\wamp\www\zipfiles7.php";
function _exec($cmd)
{
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run($cmd, 0,false);
echo $cmd;
return $oExec == 0 ? true : false;
}_exec("C:\wamp\bin\php\php5.4.3\php-win.exe -f C:\wamp\www\zipfiles7.php");
$cmd = "C:\wamp\bin\php\php5.4.3\php-win.exe -f C:\wamp\www\zipfiles7.php";
pclose(popen('start /B ' . $cmd, 'r'));
$cmd = "C:\wamp\bin\php\php5.4.3\php-win.exe -f C:\wamp\www\zipfiles7.php";
pclose(popen("start /B " . $cmd, "r"));
system("cmd /c php zipfiles7.php > test.txt 2>&1");
exec('psexec start /B -d php.exe zipfiles7.php > test.txt 2>&1');
exec('psexec -d php.exe zipfiles7.php > test.txt 2>&1');
exec('psexec -d php.exe zipfiles7.php');
$cmd = "C:\wamp\bin\php\php5.4.3\php-win.exe -f C:\wamp\www\zipfiles7.php";
exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, "test.txt", "php_error.log"));
pclose(popen('start /B php zipfiles7.php > test.txt 2>&1', 'r'));
pclose(popen("start /B C:\wamp\bin\php\php5.4.3\php-win.exe -f C:\wamp\www\zipfiles7.php", "r"));
shell_exec('start /B "C:\wamp\bin\php\php5.4.3\php.exe -f C:\wamp\www\zipfiles7.php > test.txt 2>&1"');
exec('START /B "C:\wamp\bin\php\php5.4.3\php.exe -f C:\wamp\www\zipfiles7.php"', $output);
passthru("start /B c:/wamp/bin/php/php5.4.3/php-win c:/wamp/www/zipfiles7.php > test.txt 2>&1");
exec("php.exe -f zipfiles7.php > test.txt 2>&1");
exec("php-win -f zipfiles7.php > /dell/null 2>/dev/null &", $output);
exec("start /B php zipfiles7.php > test.txt 2>&1");
exec("start php.exe -f zipfiles7.php > test.txt 2>&1");
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("cmd /C C:\wamp\www\batch.bat", 0, false);
exec("CHP.exe C:\wamp\www\batch.bat > test.txt 2>&1");
exec("start /min php zipfiles7.php > test.txt 2>&1");
shell_exec('call "cmd /c batch.bat"');
batch.bat
@echo off
"start /B bg C:\wamp\bin\php\php5.4.3\php.exe -f C:\wamp\www\zipfiles7.php > test.txt"
Additional methods tried:
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("pipe", "r") // stderr is a file to write to
);
$cmd = "start /B php.exe zipfiles7.php";
$process = proc_open($cmd, $descriptorspec, $pipes, null, null);
if (is_resource($process))
{ ....
// buffer all upcoming output
ob_start();
// get the size of the output
$size = ob_get_length();
// send headers to tell the browser to close the connection
header("Content-Length: $size");
header('Connection: close');
// flush all output
ob_end_flush();
ob_flush();
flush();
// close current session
if (session_id()) session_write_close();
exec("start /B php zipfiles7.php > test.txt 2>&1");
or
shell_exec('psexec -s -d php.exe zipfiles7.php');
or
$runCommand = 'php -q zipfiles7.php';
$WshShell = new COM(“WScript.Shell”);
$oExec = $WshShell->Run($runCommand, 7, false);
Even trying JavaScript
function background(){
var url = "C:\wamp\www\zipfiles7.php";// No question mark needed
xmlReq=new XMLHttpRequest();
xmlReq.open("POST",url,true);
xmlReq.send();
console.log("done");
}
echo '<script type="text/javascript">', 'background();', '</script>';
or
function background(){
var str = "Success";
var url = "C:\wamp\www\zipfiles7.php";
xmlReq=new XMLHttpRequest();
xmlReq.open("POST",url,true);
xmlReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlReq.setRequestHeader("Content-length", str.length);
xmlReq.setRequestHeader("Connection", "close");
xmlReq.send();
console.log("done");
}
Even Ajax isn't going to the background!
function background()
{
$.ajax(
{
type: "POST",
url: "zipfiles7.php",
data: data, // data to send to above script page if any
cache: false,
success: function(response)
{
alert('Succes');
}
});
}
Please help me understand why the process will not go to the background. Thank you in advance.