I am executing the following php code, and the status returned is -1, while output array is empty. What does this mean?
$cmd = "sort -T /var/www/html/my_app/protected/runtime/sort// -t '|' /var/www/html/my_app/protected/runtime/workflow/db-export/filename_1818.txt -o /var/www/html/my_app/protected/runtime/workflow/db-export/filename_1818.txt 2>&1";
exec($cmd, $output, $status);
print_r($output);
echo $status;
Related
I know this has been asked and answered numerous times on this site but I'm still having some trouble.
from bash:
sudo python test1.py blue
test1.py
import sys
who = sys.argv[1]
print sys.argv[1]
print who
outputs:
blue
blue
from PHP
$ledcolor = "0,255,0";
$result = exec("sudo python test1.py blue");
$result = exec("sudo python test1.py" . $ledcolor);
$result = exec('sudo python test1.py' . $ledcolor);
$result = exec('sudo python test1.py blue');
all result in no output from python
I have also tried shell_exec in PHP with same (no) results.
$result = exec("sudo python test1.py blue" . $ledcolor);
echo "\n";
print $result;
PHP Output = blue0,255,0
what am I doing wrong?
In php, the exec function passes its full output via a parameter passed by reference :
$out = '';
$cmd = exec('/bin/somecommand some parameters', $out);
echo $out;
$cmd holds the return status of the exec call, not the output.
Ref : http://php.net/manual/en/function.exec.php
You can use shell_exec instead of exec, or ` `.
$result = `sudo python test1.py blue`;
When this script is run, the image is saved to a file:
<?php
$url = "rtmp://109.71.162.112:1935/live/sd.jasminchannel.stream";
exec('avconv -i "'.$url.'" -ss 00:00:01 -t 1 -r 1 -f image2 "screenshot.jpg" 2>&1', $output, $status);
header("Content-type: image/png");
readfile("screenshot.jpg");
I wish it was returned on the screen (on the fly means it, write to variable, instead without saving to file and afterwards reading, display, deleting this file):
<?php
$url = "rtmp://109.71.162.112:1935/live/sd.jasminchannel.stream";
exec('avconv -i "'.$url.'" -ss 00:00:01 -t 1 -r 1 -f image2 $variable 2>&1', $output, $status);
header("Content-type: image/png");
echo $variable;
How can I do this?
I think this will solve your problem:
<?php
$url = "rtmp://109.71.162.112:1935/live/sd.jasminchannel.stream";
header("content-type: image/jpeg");
echo passthru('avconv -i "'.$url.'" -ss 00:00:01 -t 1 -r 1 -f image2 pipe:.jpg 2>/dev/null');
This is untested, but it should work. I've been working on a similar problem at:
https://gist.github.com/megasaturnv/a42ed77d3d08d0d3d91725dbe06a0efe
and with the image in an tag:
https://gist.github.com/megasaturnv/6e5965732d4cff91f2e976e7a39efbaa
First let's run the command via terminal:
$ echo 1; /etc/init.d/apache3 restart; echo 2;
result.. ( apache3 on purpose to see an error )
1
bash: /etc/init.d/apache3: No such file or directory
2
awsome.
now let's run this via php..
<?php
$command = "echo 1; /etc/init.d/apache3 restart; echo 2; 2>&1";
$response = shell_exec("$command");
echo $response;
?>
all I see on the browser is: 1 2
I tried all sorts of things. replaced the semi colons with "&&"..
tried all the php stuff such as..
passthru()
exec()
system()
popen()
i tried it all pretty much. been hours.. can not get it to show me the same stuff i see via terminal.
You have to use 2>&1 after the restart command
Your command :
$command = "echo 1; /etc/init.d/apache3 restart; echo 2; 2>&1";
yours has "2>&1" at the end which has no use.
Also you will add to 2>&1 after each command in case others uses STDERR
$command = "echo 1; /etc/init.d/apache3 restart 2>&1; echo 2 ";
Consider using exec. The basis function only returns the first line of the output:
$response = exec("$command"); // just the first line
But use the additional parameters to capture both the output (as an array) and the return value
$retval = NULL;
$output = NULL;
$response = shell_exec("$command", $output, $retval); // last two params are passed by reference and modified by the command
Also, as user993553 posted, the output captured by these cli functions in PHP will usually just return stdout and not stderr. You can append " 2>&1" to any given command (note the space before the 2) in order to route stderr into the output.
That said, your function becomes:
$command = "echo 1; /etc/init.d/apache3 restart 2>&1; echo 2;";
$retval = NULL;
$output = NULL;
$response = exec($command, $output, $retval);
var_dump($output);
and the output:
array(3) {
[0] =>
string(1) "1"
[1] =>
string(37) "sh: 1: /etc/init.d/apache3: not found"
[2] =>
string(1) "2"
}
EDIT: you can also check $retval for an error condition. If it's not empty or zero, then signifies an error.
From the manual of shell_exec:
ALSO, note that shell_exec() does not grab STDERR, so use "2>&1" to
redirect it to STDOUT and catch it.
I have a php page that creates a shell script that the same php page starts after creating it, inside I have one of many commands that I want to send the process to a log that does not work while others actually work....
<php
$scriptfile = script.sh;
$logfile = process.log;
$imgfile = image.ppm;
//this one works, it sends the output to the log file
$cmd ="Scripts/convert.sh file.doc > $logfile \\\n";
file_put_contents($scriptfile, $cmd, FILE_APPEND | LOCK_EX);
//this one does not work, it does not send the output to the log file and stops the process
$cmd = "&& for i in $(seq --format=%003.f 0 $(( $(ls -1 | wc -l) -1 )) ); do echo doing OCR on page \$i; tesseract $imgfile-\$i.ppm $imgfile-\$i -l eng; done >> $logfile";
file_put_contents($scriptfile, $cmd, FILE_APPEND | LOCK_EX);
$cmd = "/bin/sh $scriptfile > /dev/null 2>&1 &";
shell_exec($cmd);
?>
I have tried the not working command from the shell and it does send the output to the log file, either this way:
for i in $(seq --format=%003.f 0 $(( $(ls -1 | wc -l) -1 )) ); do echo doing OCR on page $i; tesseract image-$i.ppm image-\$i -l eng; done >> process.log
or this way:
for i in $(seq --format=%003.f 0 $(( $(ls -1 | wc -l) -1 )) ); do echo doing OCR on page $i >> process.log; tesseract image-$i.ppm image-\$i -l eng; done
And here you have the way the shell script looks like after being created by php:
#! /bin/sh
Scripts/convert.sh file.doc >> process.log \
&& for i in $(seq --format=%003.f 0 $(( $(ls -1 | wc -l) -1 )) ); do echo doing OCR on page $i; tesseract image-000.ppm image-$i -l eng; done >> process.log
So my question is, what can be wrong, I've tried many different things already but no success unfortunately, any help or advice will be very welcomed!! thanks from now!!
I'm trying to ping a bunch of IPs using PHP/HTML.
<?php
$ip=array("192.10.1.1","192.10.1.2","192.10.1.3","192.10.1.4","192.10.1.5")
$list=count($ip);
$instruction = "";
for ($x=0; $x<$list; $x++)
{
if ($x > 0)
{
$send2_bg =" & ";
}
else
{
$send2_bg = "";
}
$instruction = $instruction.$send2_bg."ping -c 2 -w 1 ". $ip[$x]." | grep -i received | awk '{print $4}'" ;
}
echo $instruction;
$result =exec($instruction);
?>
Expected output array
1 1 0 0 2
But I'm failing to get the output, The instruction is constructed perfectly but after exec(), the output is not as I expect.
exec() just returns the last line of the output
shell_exec() returns all output
Try capturing the output like this:
exec($command, $host, $output);
print_r($output);
The problem is that you are echoing the instruction var see that link - PHP manual
Please reffer to the parameter output and echo that one instead the instruction var.