I am making an online code runner and it is working well but when it is running into an infinite loop then it is not stopping, so I want to kill this child process if it is being executed more than 10 sec. How can kill the exec process after 10s?
export const javascriptExecuter = (data, input) => {
return new Promise((resolve, reject) => {
const fileName = "code.js";
saveFile(fileName, data).then(() => {
fs.writeFile("jsinput.txt", input, (err) => {
if (err) {
reject("internal server error ...");
}
});
});
setTimeout(() => {
// I WANT TO KILL THE PROCESS AFTER 10 SEC IF IT IS RUNNING **********
}, 10000);
exec("node " + fileName + " < " + "jsinput.txt", (err, stdout, stderr) => {
if (err) {
resolve({
err: true,
output: err,
error: stderr,
});
}
resolve({
err: false,
output: stdout,
});
});
});
};