I've been looking for quite a while on internet, but I cannot find a solution to my problem. I am pretty new to PHP, and I am having issues to run a php script in background.
Basically, I am getting POST data from JQuery ajax, I put it in a long string using escapeshellarg() on each variable and separated by spaces, and I call the script I want to process in background with exec() or shell_exec() functions (I tried both without any success...).
$arg_list = escapeshellarg($str_solution)." ";
$arg_list .= escapeshellarg($str_rubriques)." ";
$arg_list .= escapeshellarg($intervalle)." ";
$arg_list .= escapeshellarg($date_start)." ";
$arg_list .= escapeshellarg($date_end)." ";
$arg_list .= escapeshellarg($_SESSION['user_id'])." ";
$arg_list .= escapeshellarg($_SESSION['user_name']);
//BACKGROUND EXECUTION
$output = shell_exec("/opt/plesk/php/5.6/bin/php-cgi -q script_rapports_intervalle.php ".$arg_list." &");
print_r($output);
On the script_rapports_intervalle.php file, I just do :
print_r($argv);
print_r("here i am");
die;
//HERE GOES THE SCRIPT
//....
I do got a response from this script, as it displays only the "here i am" string. When I use a var_dump() on $argv, it says it is NULL. I have also tried to pass simple arguments like "foo" and "bar", but it seems that $argv is always equal to NULL.
Moreover, I saw many usages for processing a background script: putting "&" at the end of the command, or putting "| at now" instead. Which one should I use?
I guess I am surely doing things the wrong way, but I still cannot see where to fix the issues.
For me this simple code works
$params = array("function_name" => 'image_detect',"image_guid" => $image_guid);
$query_string = http_build_query($params, "", " ");
if (PHP_OS === "WINNT") {
pclose(popen("start /B php " . $script_location . " " . $query_string, "r"));
} else {
exec("/opt/plesk/php/5.6/bin/php " . $script_location . " " . $query_string . " &> /dev/null &");
}
So in your case you can check by replacing /opt/plesk/php/5.6/bin/php-cgi with /opt/plesk/php/5.6/bin/php
Related
I am trying to convert videos into MP4 using FFMPEG. I have it set up this way:
.
.
private $ffmpegPath;
public function __construct($con) {
$this->con = $con;
$this->ffmpegPath = realpath("ffmpeg/bin/ffmpeg.exe");
}
.
.
public function convertVideoToMp4($tempFilePath, $finalFilePath){
$cmd = "$this->ffmpegPath -i $tempFilePath $finalFilePath 2>&1";
$outputLog = array();
exec($cmd, $outputLog, $returnCode);
if($returnCode != 0){
foreach ($outputLog as $line){
echo $line."<br>";
return false;
}
}
return true;
}
And in the browser i get the following error:
'C:\xampp\htdocs\Thinksmart First Sprint' is not recognized as an internal or external command".
In my constructor i have it set up to give me the realpath and i suspect that this is what it does in the command line:
C:/xampp/htdocs/Thinksmart FIrst Sprint/ffmpeg/bin/ffmpeg.exe -i (file temp name) (file name i want)
And this should work, but i dont know why it wont. Any ideas? Its my first time working with video conversions.
As you can see, spaces in your command are used to separate arguments. So if there are spaces in a path you need to quote the entire path with quotes so that the shell/processor knows they aren't separators but are one argument:
$cmd = $cmd = '"' . $this->ffmpegPath . '" -i $tempFilePath $finalFilePath 2>&1';
Which will result in a command something like this:
"C:/xampp/htdocs/Thinksmart First Sprint/ffmpeg/bin/ffmpeg.exe" -i C:/path/to/file1 C:/path/to/file2 2>&1
I think only double-quotes work on Windows. You need to quote $tempFilePath and $finalFilePath if they might have spaces in them as well.
I am running many python scripts from PHP. My php script template as below:
<?php
setlocale(LC_ALL, "en_US.utf8");
header("Content-Type: application/json");
header("Access-Control-Allow-Origin: *");
$command = escapeshellcmd("/usr/bin/python2.7 /path/to/script");
$args = escapeshellarg($_GET["title"]). " " .
escapeshellarg($_GET["user"]);
$output = shell_exec($command . " " . $args);
echo $output;
But now I need to run some python scripts which are in virtual environment.
I tried to replace /usr/bin/python2.7 with ./www/python/venv/bin/python3, but it does not work.
So how to run it in PHP?
To really run venv you would need to do three steps in the shell:
Change into project root.
source venv/bin/activate
run python path/to/script
Prerequisite you already have prepared a virtual env for the project.
You could combine this three steps into a bash script and call this script from PHP.
Ideally You should use APIs, that is the best practice. But if you don't have API available you can use pipe. That can be used like this function: exec_command($command) where, $command = $command . " " . $args Below is the code:
<?php
setlocale(LC_ALL, "en_US.utf8");
header("Content-Type: application/json");
header("Access-Control-Allow-Origin: *");
$command = escapeshellcmd("/usr/bin/python2.7 /path/to/script");
$args = escapeshellarg($_GET["title"]). " " .
escapeshellarg($_GET["user"]);
$command = $command . " " . $args;
$output = "";
$hd = popen($command, "r");
while(!feof($hd))
{
$output .= fread($hd, 4096);
}
pclose($hd);
echo $output;
?>
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.
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
i have apc.enable_cli=1 in php.ini for cli;
i tested sharing variables with apc with this script:
<?php
$apctest=apc_fetch('apctest');
echo "apctest was " . $apctest;
echo "\n";
if($argc>1){
$newval=$argv[1];
}else{
$newval='ok';
}
echo "setting apctest to " . $newval;
echo "\n";
apc_store('apctest',$newval);
sleep(30);
i runned it with
php test_cli_apc.php > /dev/null &
and then within 30 seconds runned
php test_cli_apc.php
but it has outputted "apctest was " , not "apctest was ok"
i have tried same script with "apc" changed to "apcu" but it also does not work in same way.