I'm trying to run a python script from a PHP file. for that I'm using system() function of PHP.
system('ls -l', $retval) is working as expected but system('python CMI.py', $retval) is not giving the desired results. The python script CMI.py is running perfectly fine when run from terminal.
I will give the code snippets and outputs so that there will be a clear picture of permissions.
<?php
echo '<pre>';
// Outputs all the result of shellcommand "ls", and returns
// the last output line into $last_line. Stores the return value
// of the shell command in $retval.
$last_line = system('ls -l', $retval);
// Printing additional info
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
echo '<pre>';
// Outputs all the result of shellcommand "ls", and returns
// the last output line into $last_line. Stores the return value
// of the shell command in $retval.
$last_line = system('python CMI.py', $retval);
// Printing additional info
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
?>
This is the output in the browser
Related
I am using Phpseclib 1.0 to execute multiple commands on remote server . I know how configure it to exit on error but is there any way to setup that if there is error it will stop and execute different commands ? For example I have 8 commands to execute when it all works it's don't show any output so it's all good . if for example after 4th command it gets error I need that it stop to execute the rest commands and execute command to delete what it did as well another command to run another script to notify admins about accident. The current code I using is
<?php
include('Net/SSH2.php');
ini_set('display_errors', 1);
error_reporting(E_ALL);
$ssh = new Net_SSH2('host');
if (!$ssh->login('user', 'password')) {
exit('Login Failed');
}
echo '<pre>' . $ssh->exec('create vlan “TEST”') . '</pre>';
echo '<pre>' . $ssh->exec('configure vlan TEST tag 666') . '</pre>';
echo '<pre>' . $ssh->exec('configure vlan TEST add ports 8 untagged') . '</pre>';
echo '<pre>' . $ssh->exec('create l2vpn vpws TEST fec-id-type pseudo-wire 666') . '</pre>';
// Execute on error
echo '<pre>' . $ssh->exec('delete l2vpn vpws “TEST”') . '</pre>';
echo '<pre>' . $ssh->exec('delete vlan “TEST”') . '</pre>';
?>
You could check stderr by doing $ssh->getStdError() and seeing if it's empty. There's also $ssh->getExitStatus().
I use function system() to run system process.
$buff = system('python excel.py ' . $handle->file_dst_pathname, $retval);
It displays messages in line without separations.
In Python I use this line to print data:
print "# %d - article \"%s\" was inserted!" % (i, article)
Precede the output of system(...); with echo "<pre>"; and follow with echo "</pre>";
Or shorter:
$buff = "<pre>" . system('python excel.py ' . $handle->file_dst_pathname, $retval) . "</pre>";
You could also change all the "\n" chars of your output into "<br/>".
Is there a way to run a shell script from PHP and echo the results after progress is completed?
Here is my shell script:
(Its multilines - a few commands that have to be ran one after the other. )
cd
cd /var/www/html/
npm uninstall gulp --save
npm install gulp --save
npm start
here's my currently functioning PHP script. It only outputs some of the progress and only outputs it when complete. I need a live preview of the progress.
<?php
echo "loaded<br><br>";
// echo shell_exec("cd /var/www/html/..");
// rin the shell scrip from header_register_callback
echo '<pre>';
// Outputs all the result of shellcommand "ls", and returns
// the last output line into $last_line. Stores the return value
// of the shell command in $retval.
$last_line = system('cd /var/www/html/; npm start', $retval);
// Printing additional info
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
?>
Here is another approach. It uses redirection to output the results and then file_get_contents() to read the output.
<?php
echo "loaded<br><br>";
$cmd = 'npm start';
$run = $cmd . ' > result.txt 2> errors.txt';
$output = shell_exec($run);
$results = file_get_contents('result.txt');
if (strlen(file_get_contents('errors.txt')) != 0) {
$results .= file_get_contents('errors.txt');
}
echo "<pre>$results</pre>";
?>
I guess, ob_flush() would work:
<?php
echo "loaded<br><br>";
ob_flush();
// echo shell_exec("cd /var/www/html/..");
// rin the shell scrip from header_register_callback
echo '<pre>';
ob_flush();
// Outputs all the result of shellcommand "ls", and returns
// the last output line into $last_line. Stores the return value
// of the shell command in $retval.
$last_line = system('cd /var/www/html/; npm start', $retval);
// Printing additional info
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
?>
But I don't understand why you echo HTML-tags when you run that script on the console... Hope I got you right
I have a simple php code:
<?php echo exec('/opt/anaconda2/bin/python test2.py 2>&1'); ?><br>
And test2.py simply import a library called theano
import theano
It works under ssh but throw out:
KeyError: 'PATH'
when looking at the php in browser.
What's happeneing here? Is there any way that I can see full trace of error msg?
You can try this for the PHP side of things, but I think KeyError is a Python thing:
<?php
$output = array();
exec('/opt/anaconda2/bin/python test2.py 2>&1', $output, $returnCode);
echo 'Output is: ' . PHP_EOL;
var_dump($output);
echo 'Return code is: ' . PHP_EOL;
var_dump($returnCode);
?>
I want to convert all pages of a PDF to JPEG. The PHP-script does what it should locally but fails on our live server.
$pdf = Zend_Pdf::load($file['tmp_name']);
echo 'Pages: '.sizeof($pdf->pages).'<br />';
for($pageCounter = 0; $pageCounter < $object->getNumberOfPages(); $pageCounter++) {
$pagenumber = $pageCounter + 1;
$currentfilename = $filename . "_$pagenumber.jpg";
echo $thumbPath.$currentfilename.'<br />';
echo $file['tmp_name']."[$pageCounter]<br />";
$return; $out;
exec("convert -density 250 '".$file['tmp_name']."[$pageCounter]' -quality 60 '$bigPath$currentfilename'", $out, $return);
var_dump($return); echo '<br />'; echo var_dump($out); echo '<br />';
exec("/usr/bin/convert -density 16 '/home/data/websites/www/meonline.be/public_html/images/magazines/".$file['name']."[$pageCounter]' -quality 60 '$thumbPath$currentfilename'", $out, $return);
var_dump($return); echo '<br />';
}
I tried to refer to the convert command by explicitly pointing to /usr/bin/convert & /usr/local/bin/convert. I always get 1 (or 127 if i'm pointing to the wrong folder) and the output array is always empty.
All folder as shown exist and have mode 777 set.
Here's a summary of all echoed output:
Full exec command:
convert -density 250 '/home/data/websites/www/meonline.be/public_html/images/magazines/HogeschoolPromotorenP-studenten.pdf[0]' -quality 60 '/home/data/websites/www/meonline.be/public_html/images/magazines/big/pLjB9_HogeschoolPromotorenP-studenten.pdf/hogeschoolpromotorenp-studenten_1399449783_1.jpg'
First echo echo 'Pages: '.sizeof($pdf->pages).'<br />';
pages: 1
Second echo echo $thumbPath.$currentfilename.'<br />';
/home/data/websites/www/meonline.be/public_html/images/magazines/thumb/pLjB9_HogeschoolPromotorenP-studenten.pdf/hogeschoolpromotorenp-studenten_1399449783_1.jpg
Third echo echo $file['tmp_name']."[$pageCounter]<br />";
/home/data/websites/www/meonline.be/public_html/images/magazines/HogeschoolPromotorenP-studenten.pdf[0]
Fourth echo var_dump($return); echo '<br />'; echo var_dump($out); echo '<br />';
int(1)
array(0) { }
UPDATE
Someone executed my command in the terminal and got the following response:
convert: Postscript delegate failed `/home/data/websites/www/meonline.be/public_html/images/magazines/HogeschoolPromotorenP-studenten.pdf': No such file or directory # error/pdf.c/ReadPDFImage/713.
convert: no images defined `/home/data/websites/www/meonline.be/public_html/images/magazines/big/fs5Jd_HogeschoolPromotorenP-studenten.pdf/hogeschoolpromotorenp-studenten_1399455323_1.jpg' # error/convert.c/ConvertImageCommand/3150.
I'm 100% sure that the PDF file exists and the second line complains about the image not being defined, but this command should create them for me.
In case anyone is looking for an answer, the problem was actually quite simpel.
The local server had Ghostscript installed, the live server didn't...
Download it here and follow the instructions here.
In my case, I didn't have access to the commandline on the live server. In that case, check if /usr/bin/gs is available (in which case it is installed) with this command from PHP:
$return; $out;
exec("/usr/bin/gs 2>&1", $out, $return);
var_dump($return); echo '<br />'; echo var_dump($out); echo '<br />';