const handler = async () => {
console.log('Start cleanup')
await doCleanUpWork()
console.log('Exiting')
process.exit(0)
}
process.on('SIGTERM', handler)
process.on('SIGINT', handler)
// app.js
async function doCleanUpWork() {
return new Promise((resolve) => {
setTimeout(resolve, 3000)
})
}
const handler = async () => {
console.log('Start cleanup')
await doCleanUpWork()
console.log('Exiting')
process.exit(0)
}
process.on('SIGTERM', handler)
process.on('SIGINT', handler)
// keep running
setInterval(() => {
console.log('Working')
}, 1000)
Working
Working
^CStart cleanup
Working
Working
Working
Exiting
npm
\__ sh
\__ node
/* Ignore interrupts while waiting for a job run without job control
to finish. We don't want the shell to exit if an interrupt is
received, only if one of the jobs run is killed via SIGINT.
...
PID PPID TT SESS COMMAND
1 0 pts/0 1 bash
8 1 pts/0 1 npm
24 8 pts/0 1 \_ sh
25 24 pts/0 1 \_ node
dumb-init
\__ npm
\__ sh
\__ node
Working
Working
Start cleanup
npm
\__ node
-
dumb-init:不會等待所有子行程退出
-
docker –init 引數:不會給所有子行程轉發訊號,也不會等待子行程退出
-
baseimage 專案:提供一個 ubuntu 基礎映象,內含一個 init 程式,使用 Python 編寫,會轉發訊號和等待子行程退出,但是體積比較龐大。
-
smell-baron 專案:一個 c 寫的小程式,體積小,會轉發 signals 和等待所有子行程退出