3

What I have:

  • A program (c++) on a server (linux)
  • The program can be executed with command line parameters
  • The program finishes after a while and produces continously output ("log.txt")

What I want:

My idea is to have some kind of user interaction with that program:

  1. The user opens a website which shows some inputs (checkboxes, etc.)
  2. After submitting (POST) to the server the program should be started with chosen settings
  3. While running log.txt changes made by the program should be displayed to the user
  4. When finished a message should occur (e.g. "Done")

What would be the best approach? My current idea is to start a background process (php execute a background process ) and monitor the process id. However, I do not know how to dynamically monitor the process with php, how to show the user the log.txt output or how to tell the user that the program has finished, because these things are not static but dynamic.

I am quite fine with C++, but my html and php skills are basic. Maybe I need a further technology for this?

Community
  • 1
  • 1
Anonymous
  • 4,617
  • 9
  • 48
  • 61
  • Btw. This is NOT too broad. I am searching for a general approach for interacting between a website and a program since I have not found information about this. – Anonymous Apr 23 '15 at 13:32
  • Common database for C++ program and PHP script? PHP scripts creating files with request which C++ will search? PHP starting other program with params sanded to it which will be able to send signals or use any other IPC method? http://en.wikipedia.org/wiki/Inter-process_communication – senfen Apr 23 '15 at 13:34
  • As long as you know what kind of server you have (either linux or windows) this can be - of course - accomplished using PHP. Another quite interesting solution that you might like, but which could be a little bit more complex than that, is using a nodejs server. That will allow you to have an (almost) real time communication between the client and the server, which is something you might want to accomplish, as far as I can see ( point 3 ) – briosheje Apr 23 '15 at 13:35

1 Answers1

1
<?php 

ob_implicit_flush(true);
ob_end_flush();

$cmd = "your_program -{$_POST[option1]} -{$_POST[option2]}";

$descriptorspec = array(
   0 => array("pipe", "r"),
   1 => array("pipe", "w"),
   2 => array("pipe", "w")
);
flush();
$process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array());
echo "<pre>";
if (is_resource($process)) {
    while ($s = fgets($pipes[1])) {
        print $s;
        flush();
    }
}
echo "</pre>";

proc_close();

echo 'Proc finished!';

as answered here, but with a few modifications.

Community
  • 1
  • 1
mariobgr
  • 2,143
  • 2
  • 16
  • 31
  • This approach seems to be the right one. Instead of proc_close(); you have to use proc_close($process);. Furthermore I can see that my program has been executed (log-file occurs). Nevertheless fgets($pipes[1]) does not receive any output. My program uses std::cout, what might be wrong here? – Anonymous Apr 23 '15 at 18:33
  • Hmmm interesting issue. Do you write the output directly in the txt file, or when you execute it, you can see the output the console as well? – mariobgr Apr 23 '15 at 19:40
  • I made at least one mistake: I used clog instead of cout. Furthermore I am not sure whether adding the path to the executable has to be added to the Linux PATH. Nevertheless, after these two changes it is working! – Anonymous Apr 24 '15 at 06:50
  • You don't necessarily need to add the path to the Linux PATH, unless you want to call it directly from any directory – mariobgr Apr 24 '15 at 07:22