exec() not displaying correct output - php

I am running this test code using exec() in PHP
exec('#!/bin/bash');
exec('abc=10');
echo exec('echo $abc'); // no output
echo exec('whoami'); // this works fine
If I run the first 3 lines in terminal, the output is '10'.
But PHP does not output anything. What am I doing wrong?

http://php.net/manual/en/function.exec.php
Manual says use output_arr to capture output.
<?php
Exec ($cmd, $output_arr);
Print_r ( $output_arr);
?>
#zarathuztra is right. Try combining the various commands in a single exec with ; in between. Also check my answer if output_arr gets what you want.

Related

PHP exec() Output Cut Short

I have a PHP file that runs a node script using exec() to gather the output, like so:
$test = exec("/usr/local/bin/node /home/user/www/bin/start.js --url=https://www.example.com/");
echo $test;
It outputs a JSON string of data tied to the website in the --url paramater. It works great, but sometimes the output string is cut short.
When I run the command in the exec() script directly, I get the full output, as expected.
Why would this be? I've also tried running shell_exec() instead, but the same things happens with the output being cut short.
Is there a setting in php.ini or somewhere else to increase the size of output strings?
It appears the only way to get this working is by passing exec() to a temp file, like this:
exec("/usr/local/bin/node /home/user/www/bin/start.js --url=https://www.example.com/ > /home/user/www/uploads/json.txt");
$json = file_get_contents('/home/user/www/uploads/json.txt');
echo $json;
I would prefer to have the direct output and tried increasing output_buffering in php.ini with no change (output still gets cut off).
Definitely open to other ideas to avoid the temp file, but could also live with this and just unlink() the file on each run.
exec() only returns the last line of the output of the command you pass to it. Per the section marked Return Value of the following documentation:
The last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function.
To get the output of the executed command, be sure to set and use the output parameter.
https://www.php.net/manual/en/function.exec.php
To do what you are trying to do, you need to pass the function an array to store the output, like so:
exec("/usr/local/bin/node /home/user/www/bin/start.js --url=https://www.example.com/", $output);
echo implode("\n", $output);

PHP exec() execute last command only?

Here is an example trying to understand exec() function
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo exec("id;ls");
?>
When i run this code the result of ls only
Does exec() execute the last command only or it executes both of them and echo the last command ?
You can use shell_exec() instead for this purpose.
On the other hand, exec() returns only last line of the output (by default), but you can provide reference for output array as a second argument.
See the documentation for more info.
exec returns the last line from the result of the command. You have to use output argument. If the output argument is present, then the specified array will be filled with every line of output from the command.
exec("id;ls", $output);
var_dump($output);
you need write a shell script for Linux(excutable with .sh file)

python doesn't write if call from php

I have a python script writetest.py with the following:
print "mark1"
with open("/testfile.txt", "a") as myfile:
myfile.write("Hello World")
print "mark2"
When I call the scrip in the shell, it work and adds "Hello World" the testfile.txt. When I call it using the php exec command, it doesn't write:
exec('python writetest.py', $output, $return_var);
print_r($output);
print_r($return_var);
Which prints:
Array ( [0] => mark1 )
1
My python script runs but stops at the open code.
I think there is quote issue in your code
exec('python writetest.py")
Once you fix that, try again.
If still not working I would suggest
exec('python writetest.py', $output , $return_var)
see: http://php.net/manual/en/function.exec.php
Then print output and return_var by print_r for debugging purpose
print_r($output);
print_r($return_var);
As aynber pointed out, it was a permissions issue. I gave the web server user write access and it worked

How to display the output of a system command in browser exactly as in terminal

I used php shell_exec to run BLAST command (biologcal sequence alignment tool) and outputs the result in browser. However, I am not able to format the result same like it displayed when I run the same command in terminal . I tried using methods like passthru() and exec(). Both of it doesnt work! In my case, output formatting is important as a small space can make the error (a portion is give below). Can anyone tell me how to display the result in browser as exactly which in command terminal.
$cmd = "$blast -query /var/www/html/kim/blast/testing.txt -db /var/www/html/kim/blast/$db";
$result =shell_exec($cmd);
print_r ($result);
Part of my output looks like,
Query 707 TCAGACTTGAA 766
|||||||||||
Sbjct 3632 TCAGACTTGAA 3691
In order to keep formatting identical, including spaces etc., you should use the <pre> html element. An example:
echo '<pre>';
echo $result;
echo '</pre>';
Just echo the raw result. Using print_r or var_dump would lead to formatting by PHP. The above example is the most raw formatting you can achieve, given you leave result untouched.
With CSS you can then style the <pre>. But make sure to use a MONOSPACE font so that shell formatting is kept.

php exec() and shell_exec()

I currently have a php page that my webserver serves. In order to display all the information I need to display on the page I need output from an external python script. So I have been using the exec() command of php to execute the python script and capture the output in an array of strings as follows:
$somequery = $_GET['query'];
$result = exec("python /var/www/html/query/myscript.py ".somequery."");
//some for loop to loop through entries in result and echo them.
However there are never any entries to be printed, yet when I run the command directly on the console of the server it will output correctly. I've tried echoing out the command on the webpage that I am executing and it's the correct command. The only thing I think it can be is that exec() doesn't stop the rest of the php program from executing before it finishes, leading to the loop i have printing out entries finding that $result is empty.
How can I ensure that exec() finishes executing before the rest of my php script? Are there maybe settings in php.ini that I would need to change? I'm not entirely sure.
EDIT: I've tried running and storing the output of shell_exec("echo hello"); and printing that output, it now prints. However, when running my command that takes a few seconds longer, the program never finishes executing it before going to the next line.
EDIT 2: I found my solution in the following post https://stackoverflow.com/a/6769624 My issue was with with the numpy python package I was using and I simply needed to comment out the line in /usr/lib64/python2.7/ctypes/init.py like the poster did and my script output correctly.
The correct way to get your shell output is like this:
exec("python /var/www/html/query/myscript.py ".somequery."", $result);
var_dump($result); //output should be in here
Give it a try.

Categories