I'm using system() PHP function to run some curl commands like this system("curl command here",$output); but it displays results on screen. Any way to avoid this output?
You're using the wrong function for that. According to the docs:
system() is just like the C version of the function in that it executes the given command and outputs the result.
So it always outputs. Use execÂDocs instead which does return (and not output) the programs output:
$last = exec("curl command here", $output, $status);
$output = implode("\n", $output);
Or (just for completeness) use output bufferingÂDocs:
ob_start();
system("curl command here", $status);
$output = ob_get_clean();
You coud try using output buffering.
ob_start();
system("curl command here",$output);
$result = ob_get_contents();
ob_end_clean();
You could either modify the command string and append " 1>/dev/null 2>&1" or - more elegantly - execute the process with a pipe (see example #2).
For a more refined control over the process' file handles, you can also use proc_open().
The system function displays the output from your command, so you're out of luck there.
What you want is to change system for exec. That function will not display the command's output.
No, you should use PHP curl library
Related
I'm trying to read python output from a php webapp.
I'm using $out = shell_exec("./mytest") in the php code to launch the application, and sys.exit("returnvalue") in the python application to return the value.
The problem is that $out doesn't contain my return value.
Instead if I try with $out = shell_exec("ls"), $out variable contain the output of ls command.
If I run ./mytest from terminal it works and I can see the output on my terminal.
sys.exit("returnvalue")
Using a string with sys.exit is used to indicate an error value. So this will show returnvalue in stderr, not stdout. shell_exec() only captures stdout by default.
You probably want to use this in your Python code:
print("returnvalue")
sys.exit(0)
Alternatively, you could also use this in your PHP code to redirect stderr to stdout.
$out = shell_exec("./mytest 2>&1");
(In fact, doing both is probably best, since having stderr disappear can be quite confusing if something unexpected happens).
I was trying to execute this command
echo exec("top");
and
echo exec("/usr/bin/top");
neither works (returns blank output)
does anybody know why?
Because top is an interactive program that is meant to be run on a terminal, not be executed from a script. You are probably want to run the 'ps' command with arguments which will sort output by cpu utilization.
http://www.devdaily.com/linux/unix-linux-process-memory-sort-ps-command-cpu
You actually can call top and echo its output. Code that worked for me:
passthru('/usr/bin/top -b -n 1');
-b - running in batch mode
-n 1 - only one iteration
It probably works, but exec() doesn't return anything. Read the Manual: exec()
$output = null;
exec('top', $output);
echo $output;
But you have another problem: top doesn't exit by itself. You cannot use it here, because you need to send the interrupt-signal (just realized: q is ok too).
One solution is to make top to stop after one iteration
$output = null;
exec('top -n 1', $output);
var_dump($output);
If you want to put it in a variable :
ob_start();
passthru('/usr/bin/top -b -n 1');
$output = ob_get_clean();
ob_clean();
I used:
$cpu = preg_split('/[\s]+/', shell_exec('mpstat 1 1'));
$cpu = 100-$cpu[42];
100% minus the idle time.
I'm building a RAKEFILE and I want to display the output on a php generated page as it gets executed.
I tried using system() since the PHP docs mention this:
The system() call also tries to automatically flush the web server's output buffer after each line of output if PHP is running as a server module.
This seems to work with multiple shell comands but when I execute rake I only get the first line:
(in /Users/path/to/proj)
Any ideas?
Cheers!
Try use exec() function
exec($command, $output);
$output is an array
//retrieved data
for($out = '',$x = 0,$len = count($output); $x < $len; $x++) {
$out .= $output[$x] . "\r\n";
}
or simple:
$out = join("\r\n", $output);
The system() call also tries to automatically flush the web server's output buffer after > each line of output if PHP is running as a server module.
This means you would only get the last line of output from the return value. The example in the system() manual page shows that and it suggests to use passthru() to get raw output. I usually use exec() though.
Turs out both functions system() & exec() actually work. The generated rake output when using --verbose isn't taken into consideration though. That's why I was confused. If anyone has more extensive knowledge on the distinction, do share :)
This is my code:
$command = 'path to some script';
echo "Running command:\n $command ";
$result = array ();
exec ($command, $result);
Which results in the following:
Running command:
[here go some warning printed by the command itself]
path to some script
I.e. the error output of the script, is somehow inserted in the middle (!) of an echo command preceding it.
Ideas?
This will be down to buffered vs non-buffered io. The error output will be stderror, and the other will be stdout. Stdout is generally buffered - so if you were to force it to flush before running the script you would get the result you want.
Try this http://php.net/manual/en/function.flush.php
This is because exec does not capture standard error (stderr), per example:
exec ('/bin/echo foo > /dev/stderr', $result);
Will output foo, even though exec shouldn't output anything. You can force it to by doing:
exec ($command.' 2>&1', $result);
The reason it appears in the middle is probably because of output buffering (as #Danny explained above). The output buffer may exhaust before the end of command and therefore is flushed automatically, and a new one is started. Hence the error appearing in the middle.
I want to see the output of the following code in the web browser:
code:
<?php
$var = system('fdisk -l');
echo "$var";
?>
When I open this from a web browser there is no output in the web browser. So how can I do this? Please help!
Thanks
Puspa
you can use passthru, like so:
$somevar = passthru('echo "Testing1"');
// $somevar now == "Testing1"
or
echo passthru('echo "Testing2"');
// outputs "Testing2"
use exec('command', $output);
print_r($output);
First of all, be sure that you (=user under which php runs) are allowed to call the external program (OS access rights, safe_mode setting in php.ini). Then you have quite a few options in PHP to call programs via command line. The most common I use are:
system
This function returns false if the command failed or the last line of the returned output.
$lastLine = system('...');
shell_exec or backtick operators
This function/operator return the whole output of the command as a string.
$output = shell_exec('...'); // or:
$output = `...`;
exec
This function returns the last line of the output of the command. But you can give it a second argument that then contains all lines from the command output.
$lastLine = exec('...'); // or capturing all lines from output:
$lastLine = exec('...', $allLines);
Here is the overview of all functions for these usecases: http://de.php.net/manual/en/ref.exec.php