PHP background execution not working with include files - php

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.

Related

start .php skript asynchron over PHP and answer to requests

I try to start a .php script asynchron from an other php script.
if (substr(php_uname(), 0, 7) == "Windows"){
$cmd = "start /B php AsynchronDownload.php fileids=".$this->header->queryParams['ids'] . " uid=".$this->header->tokenData->uId . " groupId=".$this->header->tokenData->groupId . " timestamp=".$this->header->tokenData->timestamp . " state=".$this->header->tokenData->state. " token=".$this->header->tokenData->token;
pclose(popen($cmd, "r"));
} else {
$cmd = "/usr/bin/php7.2-cli AsynchronDownload.php fileids=".$this->header->queryParams['ids'] . " uid=".$this->header->tokenData->uId . " groupId=".$this->header->tokenData->groupId . " timestamp=".$this->header->tokenData->timestamp . " state=".$this->header->tokenData->state. " token=".$this->header->tokenData->token;
exec($cmd . " > /dev/null &");
}
Thats work fine. But is it possible to awnser to an Http Request, how is send to the first skript from a client in the second asynchron started skript? I found out the details about the Request are not available on the second skript.
If I try to set Headers for the Response with (for example) header('Content-type: x-zip') they are not set. headers_list() funktion returning NULL.

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

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?

php background process in windows environment

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!!!';

Categories