You can easily use exec() or shell_exec() to execute a system command, like ls -l /var/www/mysite and then output the result of the command.
How would one execute and display the result of a command that periodically prints information out to the console?
You have a simply Python script. If you run the script from the console, it simply prints out some type of information to the console every 10 seconds forever until you force-quit the script.
How could use PHP to execute this python command and somehow capture and stream the output into the browser in realtime? Is something like this possible? I'm thinking there would have to be some sort of Ajax involved but I'm not sure.
I tried doing something like this:
python myscript.py > output.txt
And then I was planning on maybe using Ajax to periodically tail or cat the content of the output.txt and display in the browser. But output.txt doesn't appear to have any content added to it until after the script has been force-quit.
You don't see any output to output.txt because it's being buffered. For python there's an option to make it line-buffered. From the manpage:
-u Force the binary I/O layers of stdin, stdout and stderr to be unbuffered. The text I/O layer will still be
line-buffered.
So your command would then become:
python -u myscript.py > output.txt
For PHP the flush function should help you.
Related
I have to run in background a shell command (that uses a ruby script from /usr/local/bin) using php (from a html/php form) and ignoring all the output.
I've tried unsuccessfully exec(), system(), shell_exec().
Also executing the script (not written by me, script link - I don't know ruby) in the terminal and redirecting the standard output to /dev/null, it keeps showing real-time infos about the download progress and speed of the download until it's completed.
I suppose it's because of the script and I've read somewhere that proc_open() can help but I don't know how to use it.
EDIT
Also adding "&" to the command (in the terminal) it keeps showing real-time infos about the download progress and speed of the download until it's completed.
/usr/local/apache2/htdocs$ ls
1.php ruby_prog.rb
1.php:
<div>hello</div>
<div>
<?php
$command = 'ruby ruby_prog.rb';
$output = [];
exec($command, $output, $return_var);
echo($output[0]);
?>
</div>
ruby_prog.rb:
puts 'world'
url:
http://localhost:8181/1.php
output in browser:
hello
world
source html:
<div>hello</div>
<div>
world</div>
You can check the value of $return_var--if it's 0, then the ruby program ran successfully; if it's non-zero, there was an error.
If you don't want any output, then you can use the command:
ruby ruby_prog.rb > /dev/null 2>&1
The > sends stdout to /dev/null, which is a sink, i.e. it acts like a blackhole, and 2>&1 says to send stderr(2) to wherever stdout(&1) goes.
I'm running a simple wget command in shell_exec()
wget_file.php
<?php
$command = "wget http://mydomain.co/media/bigbigbig.wav";
$output = shell_exec($command);
echo $output;
?>
According to http://www.php.net/shell_exec I can possibly expect an output:
"The output from the executed command or NULL if an error occurred or the command produces no output."
If I run wget_file.php from the command line, I'll see a display of the wget results.
However, if I run it from a browser, no result is given. (but the file does indeed download successfully)
I plan to execute the wget_file.php by calling via cUrl, while passing the url + path.
But would be nice to get a response from the shell_exec(), after execution is completed.
Does anyone know if I'm just missing something to get an output (running in the browser)?
If I run wget_file.php from the command line, I'll see a display of the wget results
wget doesn't output the file contents to console by default, so presumably you're talking about the status information.
I'd imagine this is output to STDERR rather than STDOUT, and it's STDOUT that shell_exec obtains for you in your PHP script:
when you run your script from the command line, you're still seeing the output because both streams are shared by the single terminal window; it's just that you're seeing it directly from wget and not from your PHP script;
in the case of passing it through Apache and to a browser to satisfy a web request, this terminal context is disconnected from the result the user sees.
In your shell command you can redirect the former to the latter:
$command = "wget http://mydomain.co/media/bigbigbig.wav 2>&1";
The comments on the documentation for shell_exec touch on this, particularly the — er — very first one!
I have a php (cli) script on one terminal. It is constantly echoing the mysql calls etc. Is there any way I can see that output from another terminal, so that I can monitor the progress of that script?
Thanks
You can
use a terminal multiplexer or
modify your PHP script to write all output to a log file, or
pipe stdout and/or stderr to a log file (ex: php myscript.php > output.log). If you use a log file, you can monitor it in real time with tail -f output.log.
I want to compare two images using Image Magick. For this I am using following command in windows command prompt
compare -verbose -metric mae image1.jpg image2.jpg difference.png
This command compares image1 and image2 and prints the resultant output to command prompt window and generates a difference.jpg file that shows difference between two images.
But when I run same command using php shell_exec() no output is returned. I know that the command is being executed by php as difference.jpg is being generated but no output is returned by the function. Similarly when i try passthrough() or system() to get output, again the command is being executed but no output is returned.But when I use builtin system commands like dir I can easily get output.
Any help will be appreciated as I am stuck on this for hours but still no success Thanks
I solved the problem. Its strange that imagemagick compare with verbose arguement does not send anything to normal command promt output. It sends output to stderr. Stderr normally receives error output. But here this command is writing normal output to stderr. As output is going to stderr so shell_exec() is unable to get it. In order to capture it in shell_exec() we will have to append 2>&1 to command. This will redirect stderr to normal command prompt output.
The ImageMagick compare command normally doesn't produce any output. You give it two input files and the name of the output file, and it quietly creates the output file.
If there's an error, it will write an error message to stderr and set a non-zero exit status. (There should be a way to get the exit status, but I don't know PHP.)
I am trying to use exec(), system(), passthru() or anything to read in the output of iscsiadm -m session, am not having much luck, and a little lost.
What I (think i) know:
It is not a sudoers or permission problem, as the results are the same in a terminal or browser (and my sudoers is already successfully setup to use iscsiadm for login/out)
Executing the following command from a terminal, iscsiadm -m session > /tmp/scsi_sess yields an empty scsi_sess file
What I need to know:
Where is the output getting sent, that I can not read it with a bash or php script but can see it in the terminal?
How can I read the output, or get output sent somewhere that I can read it?
With your syntax you're catching only the stdout. You should redirect the stderr on the stdout with
iscsiadm -m session 2>&1 /tmp/scsi_sess
Remember, when you do a redirect with > file and you still see output, that output is from stderr and not from stdout
http://en.wikipedia.org/wiki/Standard_streams