Can't run shell_exec(.php) with mysql in php script - php

Different from the other situations I found here on SO
I have the following code in the main php script
include("sql.php"); //holds all the data to mysql (and variable $db to connect)
$output = shell_exec('/usr/local/bin/php folder/another_script.php')
and in another_script.php I have this:
include_once("../sql.php"); //notice difference to previous include in the other script
$query = "some query";
$db->query($query)
$output in the first script is blank. The second script runs when alone and not called from another script. What am I missing?

The documentation says:
Execute command via shell and return the complete output as a string
It returns the output. You never use the output, hence nothing is shown. You could do this:
$output = shell_exec('/usr/local/bin/php folder/another_script.php')
echo $output;
Yet, I advise you to reconsider your design to create classes or functions to separate functionality instead of calling (quite expensive) shells to call PHP code.

Related

Return Python value to PHP and not have it display it

Currently I am executing Python scripts from PHP with:
<?php
$output = shell_exec('python ../cgi-bin/admin_login.py');
echo $output;
?>
Which is working fine. It displays all the data from the print() calls in the script.
But how can I actually return a value to PHP (and not have it display it as output)? Obviously delete that echo call but Python return doesn't seem to do anything.
The Python script is for a login process and handles all the MySQL work for that; I know limited PHP so that is why I am using Python for this.
After running the script, a result will be generated depending on if login was successful..
e.g. LOGGEDIN, EMPTYUSR, EMPTYPW, WRONGPW. All codes for what went wrong (or right) for further acting on by PHP.
e.g. if LOGGEDIN = True when returned, PHP should create a session so the rest of the site knows a user is logged in.
So what I want is a solution of running that Python from PHP and getting those return values back without displaying them on the page.
Any advice much appreciated,
Ilmiont
Don't use shell_exec(), use exec() instead, then decide what to do depending of returned code.
<?php
exec('python ../cgi-bin/admin_login.py', $output, $code);
switch($code) {
case 0:
echo $output;
break;
// other case may follow
}
?>
WARNING exec() only capture stdout, not stderr.
If you need to get full output use :
exec('python ../cgi-bin/admin_login.py 2>&1', $output, $code);

Running script and executing PHP for output

I'm looking to run a program, and for every output line it generates, execute a PHP script and pass the line content to it.
I know, pretty hard to understand. Here's an example:
Execute script -> script outputs 'Initializing script on 127.0.0.1'. Now it needs to execute a command like php5 input.php 'Initializing script on 127.0.0.1'.
Is this doable? If so, how would I go about doing this?
Edit: to clarify; I basically want command > log.txt but in stead of writing the output to that file, writing it to a PHP script as an argument
PHP is an interpreter much like Bash, Python, etc, so you can do "normal" scripting with it. For example:
#!/usr/bin/php5
<?php
echo "Hello, world!\n";
while($line = fgets(STDIN)) {
echo "> " . $line;
}
?>
Mark the file as executable, then run:
$ /program/that/generates/lines | /path/to/your/php/script
However, contrary to your original question, it sounds to me like you actually want to use JavaScript and possibly AJAX for web purposes. Sane web applications will have the said script run in the background and safely write the results to a file or stream, using AJAX to read it and update the information on the current page.

Calling Perl script with PHP, passing variables and put result into a file

I am near losing my mind cause of a perl script I want to call via PHP.
I have a PHP Form where I put in a MySQL Query which gets stored in a file and choosing some variables.
If I call any SHELL command like "top" ... everything works fine, but as soon as I try to call my perl script with variables, there are no results at all.
The file where the results should get stored stays empty.
That's the calling part from the PHP File:
if (isset($_POST['submit'])) {
$file = 'query.sql';
$query = $_POST['query'];
file_put_contents($file, $query);
$command = "perl /home/www/host/html/cgi/remote-board-exec.pl -sqlfileexec query.sql > /home/www/host/html/cgi/passthrutest.txt";
exec($command, &$ausgabe, $return_var);
There is no error message and i already tried debug things, but nothing helped :(
Are you sure that perl is being executed? Perhaps you ought to replace the command with 'which perl' just to make sure, and to use the full path to Perl. Another idea is to make sure your:
perl script is executable (use chmod)
ensure it has '#!/usr/bin/perl' (or wherever your path to perl is)
change the command to "/home/www/host/cgi/remote-board-exec.pl..." without the perl command
dump the contents of your output array ($ausgabe) as if the command fails to execute you may find out what is happening.

Is it possible to get and extract an array, if you use shell_exec function in php

I have a php script, let's call it main_script.php and if you run it through a console it returns bunch of interesting data.
I have another script, let's call it second_script.php.
in second_script.php I am using shell_exec command such as
$main_script = "php main_script.php --array=true";
$output = $shell_exec($main_script);
That will, however, return output from main_script.php
What if I want main_script.php return an array. Something like this
if ($con->console_parm['array'] ){
$whatever = array("test"=> "data son!");
return $whatever;
}
Is there a way I can execute it through second_script.php and get that array?!
If you print serialize($whatever); in the main script, and say $array = unserialize($output); in the second script, you'll basically end up with a copy of the main script's array.
Just be sure that there's no extra output from the main script (i'm not sure how whitespace, for example, will affect unserialization...but it certainly can't help). And if your array has objects in it, be sure those classes are loaded (or can be autoloaded) in the other script...or you'll end up with a bunch of "incomplete class" objects that won't be useful for much.

check cron job has run script properly - proper way to log errors in batch processing

I have set up a cronjob to run a script daily. This script pulls out a list of Ids from a database, loops through each to get more data from the database and geneates an XML file based on the data retrieved.
This seems to have run fine for the first few days, however, the list of Ids is getting bigger and today I have noticed that not all of the XML files have been generated. It seems to be random IDs that have not run. I have manually run the script to generate the XML for some of the missing IDs individually and they ran without any issues.
I am not sure how to locate the problem as the cron job is definately running, but not always generating all of the XML files. Any ideas on how I can pin point this problem and quickly find out which files have not been run.
I thought perhaps add timestart and timeend fields to the database and enter these values at the start and end of each XML generator being run, this way I could see what had run and what hadn't, but wondered if there was a better way.
set_time_limit(0);
//connect to database
$db = new msSqlConnect('dbconnect');
$select = "SELECT id FROM ProductFeeds WHERE enabled = 'True' ";
$run = mssql_query($select);
while($row = mssql_fetch_array($run)){
$arg = $row['id'];
//echo $arg . '<br />';
exec("php index.php \"$arg\"", $output);
//print_r($output);
}
My suggestion would be to add some logging to the script. A simple
error_log("Passing ID:".$arg."\n",3,"log.txt");
Can give you some info on whether the ID is being passed. If you find that that is the case, you can introduce logging to index.php to further evaluate the problem.
Btw, can you explain why you are using exec() to run a php script? Why not excute a function in the loop. This could well be the source of the problem.
Because with exec I think the process will run in the background and the loop will continue, so you could really choke you server that way, maybe that's worth trying out as well. (I think this also depends on the way of outputting:
Note: If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.
Maybe some other users can comment on this.
Turned out the apache was timing out. Therefore nothing to do with using a function or the exec() function.

Categories