From a0be1f11d337db508016d479c7a8272783d02efc Mon Sep 17 00:00:00 2001 From: yusing Date: Fri, 29 Mar 2024 21:43:43 +0000 Subject: [PATCH] script systemd auto restart on crash --- docs/binary.md | 2 ++ setup-binary.sh | 6 +++++- src/go-proxy/constants.go | 5 +++++ src/go-proxy/main.go | 16 ++++++++++------ 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/docs/binary.md b/docs/binary.md index 11d448a..5f4cdbc 100644 --- a/docs/binary.md +++ b/docs/binary.md @@ -22,6 +22,8 @@ 3. Start editing config files in `http://:8080` +4. Check logs / status with `systemctl status go-proxy` + ## Setup (alternative method) 1. Download the latest release and extract somewhere diff --git a/setup-binary.sh b/setup-binary.sh index 48586a3..ffd32b3 100644 --- a/setup-binary.sh +++ b/setup-binary.sh @@ -91,11 +91,15 @@ mkdir -p /etc/systemd/system cat < /etc/systemd/system/go-proxy.service [Unit] Description=go-proxy reverse proxy -After=network.target +After=network-online.target +Wants=network-online.target systemd-networkd-wait-online.service [Service] Type=simple ExecStart=${APP_ROOT}/bin/go-proxy WorkingDirectory=${APP_ROOT} +Environment="IS_SYSTEMD=1" +Restart=on-failure +RestartSec=1s [Install] WantedBy=multi-user.target EOF diff --git a/src/go-proxy/constants.go b/src/go-proxy/constants.go index 7ca4f29..4e194f0 100644 --- a/src/go-proxy/constants.go +++ b/src/go-proxy/constants.go @@ -175,3 +175,8 @@ var logLevel = func() logrus.Level { } return logrus.GetLevel() }() + +var isRunningAsService = func() bool { + v := os.Getenv("IS_SYSTEMD") + return v == "1" +}() \ No newline at end of file diff --git a/src/go-proxy/main.go b/src/go-proxy/main.go index 96e5383..152d108 100755 --- a/src/go-proxy/main.go +++ b/src/go-proxy/main.go @@ -19,12 +19,16 @@ func main() { args := getArgs() - logrus.SetFormatter(&logrus.TextFormatter{ - ForceColors: true, - DisableColors: false, - FullTimestamp: true, - TimestampFormat: "01-02 15:04:05", - }) + if isRunningAsService { + logrus.SetFormatter(&logrus.JSONFormatter{}) + } else { + logrus.SetFormatter(&logrus.TextFormatter{ + ForceColors: true, + DisableColors: false, + FullTimestamp: true, + TimestampFormat: "01-02 15:04:05", + }) + } if args.Command == CommandReload { err := utils.reloadServer()