import { spawn } from 'child_process'
let subproc = spawn( xxx/xxx.exe, [])
This code opens a new process of xxx.exe.
How can I destroy the child process automatically after the main process exits
import { spawn } from 'child_process'
let subproc = spawn( xxx/xxx.exe, [])
This code opens a new process of xxx.exe.
How can I destroy the child process automatically after the main process exits
import findProcess from 'find-process'
import { exec } from 'child-process'
const stop = async () => {
const yourSubProcessName = 'xxx'
if(process.platform === 'win32') {
// Consider OS type
// This is just for WinOS
exec(`taskkill /IM ${yourSubProcessName}`)
} else {
// Other OS
exec(`kill xxx`)
}
while(1) {
const processList = await findProcess('name', yourSubProcessName)
if(processList.length === 0) break
}
}
app.on('will-quit', async () => {
await stop()
});
...
This is terminating the subProcess when the main app is closing.
Listen then will-quit
event and terminate the subProcess on handler.
This code is to close the subprocess by ProcessName but you can change this as your needs.
And you should consider your running OS type.