Implementing a 'kill' button in HTML/PHP

shanehedgehog

New Member
I'm currently working on a PHP UI that gathers information, parses it, sends it off to another program, collects the output and presents it to the user.Normally it works just fine. Sometimes, though, the other program can take forever to finish (somewhere between 2 and 15 hours, I let it run overnight and it was finished in the morning). I would like to implement a KILL button that allows the user to cancel the program, since not only does it take forever to finish, but ties up the server and prevents the user from doing anything else in the meantime.The way it's currently set up is like this: page1.php gathers all the user input. page2.php checks the user input. If it's bad, it prints errors. If the input is good, it parses it and sends it to the other program with a system call. While this is happening, a 'calculating...' gif appears on screen. Once the program is finished, the output is displayed on the page.Optimally I would like the kill button to appear at the same time (and only at the same time) as the calculating gif, and I would like it to be able to kill the process (and have the output reflect the fact that the process was killed).So far I've been able to get the button to appear when and where I'd like it. However, I have not been able to get the actual kill funcitonality that I'm looking for. Currently I have it set up so that the button is made using an html button with a javascript function that executes on click. That javascript function contains php code with a system call which finds the target processes and kills them (I know the system call works properly as I can use it at my terminal window and it works just fine).Here's the relevant code:\[code\]startload("calculating");$opt->run_optimization($output);stopload("calculating");\[/code\]This puts up the calculating gif and kill button, then runs a function which makes the system call, then removes the calculating gif and kill button.\[code\]system("./optimi/optimi < $infile > $outfile");\[/code\]This is the system call to the other program (optimi).\[code\] print <<< KILLBUTTON<br/><button type="button" onclick="kill();">Kill</button><br/><script type="text/javascript">function kill(){ <?php system("ps -Af | grep optimi | grep -v grep | awk '{print $2}' | xargs sudo kill"); ?>}</script>KILLBUTTON;\[/code\]This is my kill button, which doesn't seem to do anything at all right now.I should point out that rewriting the optimi program is something I would like to avoid at all costs, considering I'm not the one who wrote it, nor am I experienced with the programming language it's written in.Thanks.
 
Back
Top