py file call successful but no output - php

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

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.

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