You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

package main
import (
	"fmt"
	"os"
	"os/signal"
	"syscall"
	"time"
)

func main() {
	
	//create channel that can receive OS signals
	ch : = make(chan os.Signal, 1)

    //Submit channel to the notify signal
	signal.Notify(ch, syscall. SIGTERM, syscall.SIGINT)


	//a go function to monitor signals in the background
	go func() {
		for sig : = range ch {
			switch sig {
			case syscall. SIGTERM:
				fmt. Println("sigterm received')
				os. Exit(0)
			case syscall.SIGINT:
				fmt.Println("sigint received')
				oS.Exit(0)
			}
		}
	}()

	time.Sleep(time.Minute)
}




  • No labels