I have very primitive web front-end for my C++ application. Client (web browser) is enters php site and fills form with parameters. Than (after post submit) php calls exec and application does its work.
Application can work longer than minute and requires pretty large amount of RAM.
Is there any possibility to detect disconnecting from client (for example closure of tab in web browser). I want to do this, because after disconnecting client will not be able to see result of computations, so I can kill application and free some RAM on server.
Thanks for any help or suggestions.
As long as the C++ program produces output while running, rather than generating all the output just prior to termination, use
passthru()
instead ofexec()
.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:
To collect the program's output, redirect the output to a file instead of
/dev/null
. I suspect you will needpcntl
installed as well asposix
for this, since the PHP manual indicates theSIGxxx
constants are defined by thepcntl
extension - although I have never had one installed without the other so I'm not sure either way.