From c1cab5b479491087c9dbc4f70363da639e7f6e54 Mon Sep 17 00:00:00 2001 From: Carlos Rodriguez-Fernandez Date: Tue, 31 Oct 2023 08:14:49 -0700 Subject: [PATCH] reload config on SIGHUP Fixes: #118 Signed-off-by: Carlos Rodriguez-Fernandez --- README.md | 2 +- main.go | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 37e9a06..1ab61ad 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ In each host group the `interval`, `network`, and `protocol` are optional. The interval Duration is in [Go time.ParseDuration()](https://golang.org/pkg/time/#ParseDuration) syntax. -NOTE: The config is only read on startup, SIGHUP is not supported (yet). +The config is read on startup, and can be reloaded with the SIGHUP signal. ## Building and running diff --git a/main.go b/main.go index 9357297..cbbcbcd 100644 --- a/main.go +++ b/main.go @@ -20,11 +20,13 @@ import ( "net/http" _ "net/http/pprof" "os" + "os/signal" "strconv" "strings" + "syscall" "time" - "github.com/prometheus-community/pro-bing" + probing "github.com/prometheus-community/pro-bing" "github.com/superq/smokeping_prober/config" "github.com/alecthomas/kingpin/v2" @@ -228,6 +230,18 @@ func main() { http.Handle("/", landingPage) } + hup := make(chan os.Signal, 1) + signal.Notify(hup, syscall.SIGHUP) + go func() { + for { + <-hup + level.Info(logger).Log("msg", "Reexecuting and reloading the configuration") + if err := syscall.Exec("/proc/self/exe", os.Args, os.Environ()); err != nil { + level.Error(logger).Log("msg", "Error reloading config", "err", err) + } + } + }() + server := &http.Server{} if err := web.ListenAndServe(server, webConfig, logger); err != nil { level.Error(logger).Log("err", err)