1// this spawns a child process that listens for SIGHUP when the 2// parent process exits, and after 200ms, sends a SIGKILL to the 3// child, in case it did not terminate. 4import { spawn } from 'child_process'; 5const watchdogCode = String.raw ` 6const pid = parseInt(process.argv[1], 10) 7process.title = 'node (foreground-child watchdog pid=' + pid + ')' 8if (!isNaN(pid)) { 9 let barked = false 10 // keepalive 11 const interval = setInterval(() => {}, 60000) 12 const bark = () => { 13 clearInterval(interval) 14 if (barked) return 15 barked = true 16 process.removeListener('SIGHUP', bark) 17 setTimeout(() => { 18 try { 19 process.kill(pid, 'SIGKILL') 20 setTimeout(() => process.exit(), 200) 21 } catch (_) {} 22 }, 500) 23 }) 24 process.on('SIGHUP', bark) 25} 26`; 27export const watchdog = (child) => { 28 let dogExited = false; 29 const dog = spawn(process.execPath, ['-e', watchdogCode, String(child.pid)], { 30 stdio: 'ignore', 31 }); 32 dog.on('exit', () => (dogExited = true)); 33 child.on('exit', () => { 34 if (!dogExited) 35 dog.kill('SIGTERM'); 36 }); 37 return dog; 38}; 39//# sourceMappingURL=watchdog.js.map