Skip to content

Instantly share code, notes, and snippets.

@winnab
Created September 7, 2017 08:25
Show Gist options
  • Save winnab/943103628ca1d32e2442912828ea86bd to your computer and use it in GitHub Desktop.
Save winnab/943103628ca1d32e2442912828ea86bd to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
)
const fileName string = "/tmp/signal.out"
func main() {
pid := os.Getpid()
logMsg(fmt.Sprintf("pid: %d\n", pid))
sigChan := make(chan os.Signal, 1)
done := make(chan bool, 1)
signal.Notify(sigChan, syscall.SIGTERM)
go func() {
<-sigChan
logMsg(fmt.Sprintf("%d Received terminate signal\n", pid))
done <- true
}()
logMsg(fmt.Sprintln("waiting on signal"))
<-done
logMsg(fmt.Sprintf("%d exiting\n", pid))
}
func logMsg(msg string) {
file, err := os.OpenFile(fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
_, err = file.WriteString(msg)
if err != nil {
log.Fatal(err)
}
err = file.Close()
if err != nil {
log.Fatal(err)
}
}
@winnab
Copy link
Author

winnab commented Sep 7, 2017

This needs to be cross-compiled for linux as follows:

GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go build signal.go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment