As long as the C++ program produces output while running, rather than generating all the output just prior to termination, use passthru()
instead of exec()
.
This causes PHP to flush the output to the client as the content is produced, which allows PHP to detect when clients disconnect. PHP will terminate when the client disconnects and kill the child process immediately (as long as ignore_user_abort()
is not set).
Example:
<?php
function exec_unix_bg ($cmd) {
// Executes $cmd in the background and returns the PID as an integer
return (int) exec("$cmd > /dev/null 2>&1 & echo $!");
}
function pid_exists ($pid) {
// Checks whether a process with ID $pid is running
// There is probably a better way to do this
return (bool) trim(exec("ps | grep \"^$pid \""));
}
$cmd = "/path/to/your/cpp arg_1 arg_2 arg_n";
// Start the C++ program
$pid = exec_unix_bg($cmd);
// Ignore user aborts to allow us to dispatch a signal to the child
ignore_user_abort(1);
// Loop until the program completes
while (pid_exists($pid)) {
// Push some harmless data to the client
echo " ";
flush();
// Check whether the client has disconnected
if (connection_aborted()) {
posix_kill($pid, SIGTERM); // Or SIGKILL, or whatever
exit;
}
// Could be done better? Only here to prevent runaway CPU
sleep(1);
}
// The process has finished. Do your thang here.
To collect the program's output, redirect the output to a file instead of /dev/null
. I suspect you will need pcntl
installed as well as posix
for this, since the PHP manual indicates the SIGxxx
constants are defined by the pcntl
extension - although I have never had one installed without the other so I'm not sure either way.