From 53f7c0b2df88e70e1174d3be88792b858971c0a1 Mon Sep 17 00:00:00 2001 From: Ainar Garipov Date: Mon, 13 Sep 2021 17:06:16 +0300 Subject: [PATCH] Pull request: home: imp systemd unit script Updates #3579. Squashed commit of the following: commit 9bb45127f625d4df2f4df8bd2ca4d6fe83557fc8 Author: Ainar Garipov Date: Mon Sep 13 16:57:43 2021 +0300 all: imp chlog commit d2285cb6386acc5d6e835f85f118a959a33391e6 Author: Ainar Garipov Date: Mon Sep 13 16:41:54 2021 +0300 home: imp systemd unit script --- CHANGELOG.md | 3 +++ internal/home/service.go | 30 +++++++++++++++++++++++------- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8afef5a2..095b471f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,8 @@ and this project adheres to ### Changed +- The `systemd` service script will now create the `/var/log` directory when it + doesn't exist ([#3579]). - Items in allowed clients, disallowed clients, and blocked hosts lists must be unique ([#3419]). - The TLS private key previously saved as a string isn't shown in API responses @@ -191,6 +193,7 @@ In this release, the schema version has changed from 10 to 12. [#3551]: https://github.com/AdguardTeam/AdGuardHome/issues/3551 [#3564]: https://github.com/AdguardTeam/AdGuardHome/issues/3564 [#3568]: https://github.com/AdguardTeam/AdGuardHome/issues/3568 +[#3579]: https://github.com/AdguardTeam/AdGuardHome/issues/3579 diff --git a/internal/home/service.go b/internal/home/service.go index 2ba8c114..b5515ce4 100644 --- a/internal/home/service.go +++ b/internal/home/service.go @@ -310,10 +310,15 @@ func configureService(c *service.Config) { // This key is used to start the job as soon as it has been loaded. For daemons this means execution at boot time, for agents execution at login. c.Option["RunAtLoad"] = true - // POSIX + // POSIX / systemd - // Redirect StdErr & StdOut to files. + // Redirect stderr and stdout to files. Make sure we always restart. + // Start only once network is up on Linux/systemd. c.Option["LogOutput"] = true + c.Option["Restart"] = "always" + c.Dependencies = []string{ + "After=syslog.target network-online.target", + } // Use modified service file templates. c.Option["SystemdScript"] = systemdScript @@ -368,17 +373,26 @@ var launchdConfig = ` ` -// Note: we should keep it in sync with the template from service_systemd_linux.go file -// Add "After=" setting for systemd service file, because we must be started only after network is online -// Set "RestartSec" to 10 +// systemdScript is an improved version of the systemd script originally from +// the systemdScript constant in file service_systemd_linux.go in module +// github.com/kardianos/service. The following changes have been made: +// +// 1. The RestartSec setting is set to a lower value of 10 to make sure we +// always restart quickly. +// +// 2. The ExecStartPre setting is added to make sure that the log directory is +// always created to prevent the 209/STDOUT errors. +// const systemdScript = `[Unit] Description={{.Description}} ConditionFileIsExecutable={{.Path|cmdEscape}} -After=syslog.target network-online.target +{{range $i, $dep := .Dependencies}} +{{$dep}} {{end}} [Service] StartLimitInterval=5 StartLimitBurst=10 +ExecStartPre=mkdir -p /var/log/ ExecStart={{.Path|cmdEscape}}{{range .Arguments}} {{.|cmd}}{{end}} {{if .ChRoot}}RootDirectory={{.ChRoot|cmd}}{{end}} {{if .WorkingDirectory}}WorkingDirectory={{.WorkingDirectory|cmdEscape}}{{end}} @@ -389,7 +403,9 @@ ExecStart={{.Path|cmdEscape}}{{range .Arguments}} {{.|cmd}}{{end}} StandardOutput=file:/var/log/{{.Name}}.out StandardError=file:/var/log/{{.Name}}.err {{- end}} -Restart=always +{{if gt .LimitNOFILE -1 }}LimitNOFILE={{.LimitNOFILE}}{{end}} +{{if .Restart}}Restart={{.Restart}}{{end}} +{{if .SuccessExitStatus}}SuccessExitStatus={{.SuccessExitStatus}}{{end}} RestartSec=10 EnvironmentFile=-/etc/sysconfig/{{.Name}}