how to get php cli processes share variables via apc - php

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.

Related

shell_exec does not execute from browser requests

After the user submits a data via POST, I show a temporary page and do the process on the background with shell_exec, atleast that's what I'm trying to do.
Here's my page setup:
C:\laragon\www\index.php
<?php
try {
shell_exec("php " . __DIR__ . "/test.php");
} catch(Exception $e) {
echo "Exception: " . $e;
}
?>
C:\laragon\www\test.php
<?php
$myfile = fopen(__DIR__ . "/testfile.txt", "w");
echo "test";
?>
If I go to localhost or localhost/index.php, the second script doesn't run. However, when I try to call both scripts from cmd, it works with both of them.
php C:\laragon\www\index.php
php C:\laragon\www\test.php
They both work and create a file called testfile.txt
Your webserver runs as a specific user and needs the path to php.exe as there is no path environment variable for the webserver user:
shell_exec("c:\path\to\php " . __DIR__ . "/test.php");

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 passing parameters to a background script

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

Executing PHP scripts using crontab

I am trying to execute a PHP script using crontab but it doesn't seem to work. I am running AWS EC2 Linux and here is the PHP script:
FOREACH (GLOB("*.jpg") AS $filename) {
ECHO "$filename size " . FILESIZE($filename) . "\n";
UNLINK($filename);
}
FOREACH (GLOB("*.jpeg") AS $filename) {
ECHO "$filename size " . FILESIZE($filename) . "\n";
UNLINK($filename);
}
FOREACH (GLOB("*.gif") AS $filename) {
ECHO "$filename size " . FILESIZE($filename) . "\n";
UNLINK($filename);
}
FOREACH (GLOB("*.png") AS $filename) {
ECHO "$filename size " . FILESIZE($filename) . "\n";
UNLINK($filename);
}
When I execute this script manually from a browser, it works normally. But it doesn't work using crontab
Here is my cron command:
00 * * * * php /var/www/html/*****/*****/delete.php
And here is the log:
Nov 28 02:12:01 ip-##-##-##-## CROND[#####]: (root) CMD (/usr/bin/php
/var/www/html/*****/*****/delete.php)
What am I possibly doing wrong?
I just had a similar issue...
Cron is setup correct (I think) but is not running
It is now working for me:
edit /etc/crontab directly and be sure to check your paths.
(You can even cd into the correct directory like this, perhaps that will solve your issue too..)
00 * * * * cd /var/www/html/*****/*****/ && php ./delete.php
Also check if just php will do the trick.
call:
which php
to see the full path of PHP and use that instead.
If it is working fine in browser then you can set the cron job as below,
00 * * * * wget -q -O /dev/null http://example.com/delete.php
this will work same like you calling in browser just change the "http://example.com/delete.php" with the url which you call in browser.

fuse python script not running in background using php

i am trying to run a file sytem for dropbox ff4d ( from github) in background using php
the purpose if that user will get his dropbox files mount on the server and then i will give the path to a web based explorer (like eXtplorer) so user can manage his file
the script is working fine when using command shell
but when i using the exec function it working printing out the last line of the command shell
and that it . i can not get the folder mount
here the code in php :
$folder = $_POST['foldername'];
$oldumask = umask(0000);
$path = "/var/www/cloudsite/" . $folder;
if(mkdir($path, 0777, true)) {
echo $path . " success directory created ";
} else {
echo $path . "error directory not created ";
}
umask($oldumask);
#$command = '/usr/bin/python /var/www/cloudsite/ff4d/./ff4d.py '. $path .'c7ZYof8Bl0YAAAAAAAAAARThZwAUexbukmv3rMEprPJWFcoQSKGWtWHQBYM40OgC';
$result = exec($command);
if ($result){
echo " </br> execution success";
}else{
echo "</br> error not success";
}
echo $result;
and here what i get in the browser it seems like it working but just hang here nothing mount in the created directory :
var/www/cloudsite/chao success directory created
execution successStarting FUSE...
Within the latest release of my ff4d.py script I've added a "background" switch (-bg).
https://github.com/realriot/ff4d
BTW: Please remove your access key (and revoke it afterwards) because it holds your personal information and ALL your data...

Categories