
const handler = async () => {console.log('Start cleanup')await doCleanUpWork()console.log('Exiting')process.exit(0)}process.on('SIGTERM', handler)process.on('SIGINT', handler)
// app.jsasync 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 runningsetInterval(() => {console.log('Working')}, 1000)
WorkingWorking^CStart cleanupWorkingWorkingWorkingExiting
npm\__ sh\__ node
/* Ignore interrupts while waiting for a job run without job controlto finish. We don't want the shell to exit if an interrupt isreceived, only if one of the jobs run is killed via SIGINT....
PID PPID TT SESS COMMAND1 0 pts/0 1 bash8 1 pts/0 1 npm24 8 pts/0 1 \_ sh25 24 pts/0 1 \_ node
dumb-init\__ npm\__ sh\__ node
WorkingWorkingStart cleanup
npm\__ node
-
dumb-init:不會等待所有子行程退出
-
docker –init 引數:不會給所有子行程轉發訊號,也不會等待子行程退出
-
baseimage 專案:提供一個 ubuntu 基礎映象,內含一個 init 程式,使用 Python 編寫,會轉發訊號和等待子行程退出,但是體積比較龐大。
-
smell-baron 專案:一個 c 寫的小程式,體積小,會轉發 signals 和等待所有子行程退出
知識星球