I am running a shell script (myscript.sh)
using php.
myscript.sh
is contains complicated commands, more than 500 commands including loops, creating database, running java jar files, storing some data in CSV files... and many other functions.
Using PHP, I have index.php page
that contains a form with some values,
when user submit the form it forward the user to display.php page
that run the bash script.
index.php page
contains a form and a cancel button
<form action="display.php" method="post" id="myForm">
<label><b>Number of runs: </b></label>
<select class="form-control" name="runningOcc" id="runningOcc" >
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<button type="submit" name="submitPara"> Run </button>
</form>
<button type="button" name="Cancel"> Cancel Running </button>
display.php page
running (myscript.sh) using these lines:
if( isset($_POST["submitPara"]) ){
$runningOcc=$_POST["runningOcc"];
$contents = file_get_contents('myscript.sh');
$contents=str_replace("Occur_Run", $runningOcc, $contents);
$output = null;
$return_var = null;
$contents = escapeshellarg($contents);
exec("bash -c $contents 2>&1", $output, $return_var);
print_r($contents);
echo "<hr>";
print_r($return_var);
echo "<hr>";
print_r($output);
}
When the user click submit button, the script start execution in the background while the user still see the index page. When commands finish and return results the display page appear.
I want when the user click the cancel button the program stop all running shell commands, how could I do that?