2

I have a working php script using shell_exec to execute an external program that take about 30 seconds to complete. Problem is that if an user close the browser or the connection is closed for some reason the program executed with shell_exec continue to run for nothing, since it's output can't be sent to the user anymore.

Is there a way to kill this process as soon as the connection is closed?

Thank you for your help.

Marco Martinelli
  • 892
  • 2
  • 14
  • 27
  • Just to confirm I got this straight: does the user currently have to wait about 30 seconds for the page to load? – Jon Apr 19 '13 at 11:22
  • shell_exec invoke a program to do some 3D rendering and the result is the url of the rendered image, which is then set as src of a img tag. The php code that use shell_exec is executed through an ajax call so the user don't have to wait 30 seconds for a full page load, he need to wait for the result of a 3D rendering, which he know would take a while. – Marco Martinelli Apr 19 '13 at 13:15

3 Answers3

3

Finally I've solved it!

I've used the PHP function connection_aborted() to check if the client is disconnected. http://us2.php.net/manual/en/function.connection-aborted.php

The algorithm is the following (pseudocode)

shell_exec("script");//script is a shell script that launch the program in another thread thus this return immediately
$task_completed=false;
while(!$task_completed){
    if(connection_aborted()==1){
        kill_the_process();//this will kill the running process launched by script
        die();
    }
    $task_completed=...;//true if the task completed
    sleep(1);
}
Marco Martinelli
  • 892
  • 2
  • 14
  • 27
0

You could make a manager that runs/executes/stops the running php scripts. This script would run constantly -maybe as daemon- and check the status of the script/process, the user status and close the script if the user is gone. I think you could do this well with Sockets.
How to Use Sockets in JavaScript\HTML?
http://php.net/manual/en/book.sockets.php

Community
  • 1
  • 1
Anyone
  • 2,814
  • 1
  • 22
  • 27
0

you can use to get the list of all running process

$res = shell_exec('ps -x');
$res = explode("\n",$res);
print_r($res);

and use this to kill process with given ProcessID

shell_exec('kill -KILL ProcessID');
Shushant
  • 1,625
  • 1
  • 13
  • 23
  • What does this help, if he wants the script to stop? `shell_exec` is firstly not asynchronous (not counting nohup), and second of all, it still wouldn't know if the user quit the page or not. – h2ooooooo Apr 19 '13 at 11:34
  • he can use this code to kill running process when ever user close their browser by making callback function that will make ajax request whenever user close tab/browser look at http://www.w3schools.com/jsref/event_onunload.asp – Shushant Apr 19 '13 at 11:45
  • You cannot expect an onunload event to fire an ajax call before the user closes the browser. It's very unreliable. – h2ooooooo Apr 19 '13 at 12:28
  • unfortunately this doesn't cover the case of a connection closed for other reasons (e.g. a network fault) and I also think that h2ooooooo is right, onunload it's not reliable. – Marco Martinelli Apr 19 '13 at 13:20