php background process in windows environment - php

am using windows-7 OS and wamp server.
i have 2 php files trigger.php,background.php.
i want to run background.php in background .i have to call this file from trigger.php .
i tried below methods.
i added this code in trigger.php
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("C:\wamp\bin\php\php5.3.5\php-win.exe -f C:/wamp/www/background.php", 0, false);
but my background.php is not getting called.
how i can do this?
any suggestions are appreciated.

function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}
in this case your $cmd would be "php C:/wamp/www/path/to/background.php"

i changed the function as below,
$cmd = 'C:\wamp\bin\php\php5.3.5\php.exe C:\wamp\www\email3.php';
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B " . $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}
and , its works for me. :)

Update:
function execInBackground(string $cmd)
{
if (substr(php_uname(), 0, 7) == "Windows") {
pclose(popen("start /B {$cmd}", "r"));
return;
}
exec("{$cmd} > /dev/null &");
}
$phpBinPath = defined('PHP_BINARY') ? PHP_BINARY : '/usr/bin/php';
$fileToRun = __DIR__ . '/sleep.php';
$cmd = "'{$phpBinPath}' '{$fileToRun}'";
execInBackground($cmd);
echo 'Executou!!!';

Related

py file call successful but no output

im trying to execute an py file from php, using bat file .
the whole code works fine and im trying to execute an file name dbConn.py
the file executes when seperatly double clicked i.e executed and output is reflected
the code from php is this:
if ($conn->query($sql) === TRUE) {
mysqli_close($conn);
$cmd = "run_dbConn.bat" ;
execInBackground($cmd);
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
This is code for runcmd.php
<?php
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}
?>
and code for run_dbConn.bat
cd C:\xampp\htdocs\eclipse_workspace\Project
python dbConn.py
ECHO %1
echo
even the above code make the file to run , which i have checked from task bar. but the output is not generated or reflected. the dbConn.py does not have something related to code and can be executed separately....
plzz help me out even a suggestion will be well.
exec($cmd . " > /dev/null &");
This line will run the command and redirect the output to a null device, aka hides the output.
Try removing
> /dev/null

PHP background execution not working with include files

This is my code
sample.php
//Background execution
function execInBackground($cmd)
{
if (substr(php_uname(), 0, 7) == "Windows") {
pclose(popen("start /B " . $cmd, "r"));
} else {
exec($cmd . " > /dev/null &");
}
}
and
execInBackground('php -f ' . SITE_PATH . 'admin/background_process.php live_import');
background_process.php
if($argv[1]=='live_import') {
...
...
some functionalities
}
Its working fine, but in background_process.php, If I include another file like
background_process.php
if($argv[1]=='live_import') {
include_once('new.php');
}
In new.php, I have code for functionalities. But this is not working.

shell_exec blocking the thread not proceeding to next statements in windows

am executing a shell command like below.
shell_exec('java -jar sanityTest.jar');
$success = array('status' => "Success",'type' => "execute");
echo json_encode($success);
The shell_exec command not going to next statement until the execution complete. What I want is to execute it in background, even for Windows.
I tried
shell_exec('java -jar sanityTest.jar >/dev/null 2>/dev/null &');
which is coming to next line but not executing the command.
My solution: use start /B *my command*
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
} else {
exec($cmd . " > /dev/null &");
}
}
I could have found it by looking at the right keywords: https://stackoverflow.com/a/21031259/6019417

How to pass parameters to the PHP exec()

I am using the following code to run the function in background.
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null 2>&1 &");
}
}
$cmd = execInBackground("http://example.com/parentdesc/frontend/web/index.php?r=site%2Ftest");
It is working fine. But how can I pass the parameters to the exec?
I tried:
$cmd = execInBackground("http://example.com/parentdesc/frontend/web/index.php?r=site%2Ftest \"param1\" \"param2\" ");
And in my function I print the $argv. But it doesn't have any values in it.
My function:
public function actionTest()
{
// does not have anything
print_r($argv);
// here is my mail sending logic
}
Am I missing something?

popen doesn't run a proces

I have a function that should run a process in background
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen('start /B '.$cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}
I try to run script
$cmd = "php ..\runffmpeg.php";
execInBackground($cmd);
But it does nothing. When I try to run
$cmd = 'ffmpeg -i video_in.mp4 video_out.avi';
execInBackground($cmd);
Its all right. And when I try to run
exec("php ..\runffmpeg.php");
It's also all right.
So, pclose(popen('start /B php ..\runffmpeg.php', "r")); doesn't run a command. Whats a problem?
I'm using Windows and php 5.4.7
You can use: $cmd= include("php ..\runffmpeg.php");
hmm; forget process mgmt and just popen() the resource.
If it is a daemon, you get access to it, else a subprocess is created/destroyed with the popen()/pclose().

Categories