From 5f07da4854e73181e21c7d1d90191db5d65f2675 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Thu, 17 Dec 2020 12:59:05 -0800 Subject: [PATCH] util/systemd: don't log warnings when not running under systemd It caused our integration tests to fail, which prohibit logging to os.Stderr for test cleanliness reasons. --- util/systemd/systemd_linux.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/util/systemd/systemd_linux.go b/util/systemd/systemd_linux.go index 133bf520f..f33c35730 100644 --- a/util/systemd/systemd_linux.go +++ b/util/systemd/systemd_linux.go @@ -8,6 +8,7 @@ package systemd import ( "log" + "os" "sync" "github.com/mdlayher/sdnotify" @@ -35,8 +36,13 @@ var ( func notifier() *sdnotify.Notifier { getNotifyOnce.Do(func() { + sock := os.Getenv(sdnotify.Socket) + if sock == "" { + // Not running under systemd probably. Bail out before logging. + return + } var err error - getNotifyOnce.v, err = sdnotify.New() + getNotifyOnce.v, err = sdnotify.Open(sock) if err != nil { log.Printf("systemd: systemd-notifier error: %v", err) }