I am having to accomplish a custom terminal using PHP/JS/Python on a LAMP stack server to be able to execute scripts. I am able to: generate the command to be executed, run the script, and return the output. There are scripts that must be run that have a continuous (endless loop) that are to be killed using kill PID. However, I need to be able to call the output from the Python script that is running in the background. Example:
/*** PHP ***/
<?php
// AJAX call return
$data = (object) array();
$data->command = $_POST['command'];
exec('python3 ' . $data->command . ' > /dev/null &', $data->output, $data->err);
die(json_encore($data));
?>
/*** HTML ***/
<HTML>
<head>
function submitForm(){
//... function submits and returns json_object using AJAX
var data = JSON.parse(__return__);
var pre = document.createElement("PRE");
for(var index = 0; index < data.output.length; index++){
pre.innerHTML += data.output[index] + "<br>";
}
document.getElementById("output").append(pre);
}
</head>
<body>
<div id='output'></div>
<input type='TEXT' id='command' onchange='submitForm();'/>
</body>
</HTML>
/*** PYTHON ***/
#!/usr/bin/env python3
import time;
n = 0;
while n < 5:
print("THIS IS THE OUTPUT I NEED TO RETURN TO JSON");
time.sleep(1);
Issue: When I send the command as a single command, it returns the output; when I send the command to start a script, it returns null; how can I call the output as a JSON object to get the python script output that is run from the python script above.