-
Notifications
You must be signed in to change notification settings - Fork 2
Open
Description
if we run app in docker container, when container stops, docker engine will send a signal (SIGTERM) (value: 15) to the process which pid equals 1.
how a about we add shutdown-hook to thego-cli. example:
app := cli.NewApp()
app.OnTerm = func (sigValue int) {
fmt.Println("关闭文件")
}here is a tip shows how golang handle signals:
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
)
func main() {
fmt.Println("pid is ", os.Getpid())
sigs := make(chan os.Signal, 1)
exit := make(chan bool, 1)
signal.Notify(sigs, syscall.SIGTERM)
go func() {
select {
case <-sigs:
fmt.Println("关闭文件")
exit <- true
}
}()
select {
case <-exit:
break
}
close(sigs)
close(exit)
}Metadata
Metadata
Assignees
Labels
No labels