V
V
Vladimir Kudinov2012-03-08 19:40:30
PHP
Vladimir Kudinov, 2012-03-08 19:40:30

Need help from web developers

In general, this scenario:
1. there is a page in php (something like a web interface)
2. there is a program on the server that is launched from this web interface via shell_exec ()
3. the program gives out some information as it runs ( for example, random numbers one after another)
4. you need to display them on the page as these numbers are issued.

Actually, the question is: can this be somehow organized in php (maybe using JS)?
PS: naturally, the output is needed without reloading the page ala AJAX

Answer the question

In order to leave comments, you need to log in

6 answer(s)
A
Alexey, 2012-03-08
@frux

There is a simple option, there is a complex one.
Simple: execute the command via proc_open(), read from the stream opened by the command and issue it to the page using echo. After each issue, do a flush() to send the result to the page. In this case, the page itself will hang “underloaded”, the connection with the server will be opened and new data will be loaded as the script runs. Plus - in simplicity, minuses in layout restrictions (the simpler the page - the better). If the browser, intermediate proxy or web server is configured to drop an open connection after a while, the method will work on not very long commands.
More complex options are related to AJAX: either we open the same connection already from JavaScript and load the results onto the page - this way you can make a more complex layout, or we do it very difficult, add the result of the command execution to a file on the server, write a php script, which in parts gives it to our JavaScript, which renders it on the page.

I
Ivan Shalganov, 2012-03-08
@Aco

Why didn't you like passthru ?

<?php

passthru('somecommand --exec');

?>

The output will go in real time to the screen

M
Max, 2012-03-08
@7workers

let the program output random numbers to a file, and ajax periodically reads and shows this file, through a php script, if necessary.

E
Evgeny Mikhalev, 2012-03-08
@neznae4ko

The following thoughts come right off the bat:
1) If I'm not mistaken, then when you run the program through shell_exec in php, until the program runs and ends, it will not give the result in php, therefore you will get the entire array in php at once.
2) perhaps something like this will help you watch tail -n 5 /var/log/syslog.log
3) It may be easier for you to set up your program so that it writes a text file, the name of which will be given to it by the php script, and then the php script will read this file in a loop

V
Vladimir Kudinov, 2012-03-09
@frux

Maybe I'm doing something wrong, but I don't have realtime...(
PHP:

<?
$result = passthru('bash /etc/startup/start_2.sh');
echo($result);
?>

Program:
echo 10
sleep 3
echo 5
sleep 4
echo 5

V
Vladimir Chernyshev, 2012-03-09
@VolCh

In general system('bash /etc/startup/start_2.sh'), it does what you need, but as mentioned above, there are nuances with web server configs. For example, if you have nginx before apache+mod_php, then I can't predict the behavior.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question