/signalhub

handle `os/signals` with function style

Primary LanguageGoDo What The F*ck You Want To Public LicenseWTFPL

GoDoc License Go Report Coverage

Introduction

Hanlde os/signals with function style. See:

Usage

import (
	"log"
	"os"
	"syscall"
	"time"

	"github.com/cupen/signalhub"
)

func main() {
	exitHook := func(sig os.Signal) {
		// do something before process exit
		countDown(10)
		log.Printf("exited by os/signal(%v)", sig)
		os.Exit(0)
	}
	h := signalhub.New()
	h.Watch(syscall.SIGQUIT, exitHook)
	h.Watch(syscall.SIGTERM, exitHook)
	h.Watch(syscall.SIGINT, func(sig os.Signal) {
		exitHook(sig)
	})

	log.Printf("started. CTRL-C or kill -INT ${pid}")
	h.Run()
}

func countDown(secs int) {
	for i := secs; i > 0; i-- {
		log.Printf("exit after %d seconds.", i)
		time.Sleep(time.Second)
	}
}

License

        DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 
                    Version 2, December 2004 

 Copyright (C) 2020-2022 cupen <xcupen@gmail.com> 

 Everyone is permitted to copy and distribute verbatim or modified 
 copies of this license document, and changing it is allowed as long 
 as the name is changed. 

            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 
   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 

  0. You just DO WHAT THE FUCK YOU WANT TO.