Fix restart command when ctrld service was already stopped

This commit is contained in:
Cuong Manh Le
2023-08-09 02:45:38 +00:00
committed by Cuong Manh Le
parent 125b4b6077
commit 854a244ebb
3 changed files with 62 additions and 8 deletions

View File

@@ -550,13 +550,10 @@ func initCLI() {
return
}
initLogging()
tasks := []task{{s.Restart, true}}
// On Windows, s.Restart will return error unless service is running.
if runtime.GOOS == "windows" {
tasks = []task{
{s.Start, false},
{s.Restart, true},
}
tasks := []task{
{s.Stop, false},
{s.Start, true},
}
if doTasks(tasks) {
dir, err := userHomeDir()

View File

@@ -63,6 +63,16 @@ func (s *sysV) Stop() error {
return err
}
func (s *sysV) Restart() error {
if !s.installed() {
return service.ErrNotInstalled
}
// We don't care about error returned by s.Stop,
// because the service may already be stopped.
_ = s.Stop()
return s.Start()
}
func (s *sysV) Status() (service.Status, error) {
if !s.installed() {
return service.StatusUnknown, service.ErrNotInstalled

View File

@@ -27,7 +27,8 @@ func New(cfg *ctrld.Config) *Synology {
return &Synology{cfg: cfg}
}
func (s *Synology) ConfigureService(config *service.Config) error {
func (s *Synology) ConfigureService(svc *service.Config) error {
svc.Option["UpstartScript"] = upstartScript
return nil
}
@@ -86,3 +87,49 @@ func restartDNSMasq() error {
}
return nil
}
// Copied from https://github.com/kardianos/service/blob/6fe2824ee8248e776b0f8be39aaeff45a45a4f6c/service_upstart_linux.go#L232
// With modification to wait for dhcpserver started before ctrld.
// The upstart script should stop with an INT or the Go runtime will terminate
// the program before the Stop handler can run.
const upstartScript = `# {{.Description}}
{{if .DisplayName}}description "{{.DisplayName}}"{{end}}
{{if .HasKillStanza}}kill signal INT{{end}}
{{if .ChRoot}}chroot {{.ChRoot}}{{end}}
{{if .WorkingDirectory}}chdir {{.WorkingDirectory}}{{end}}
start on filesystem or runlevel [2345]
stop on runlevel [!2345]
start on started dhcpserver
{{if and .UserName .HasSetUIDStanza}}setuid {{.UserName}}{{end}}
respawn
respawn limit 10 5
umask 022
console none
pre-start script
test -x {{.Path}} || { stop; exit 0; }
end script
# Start
script
{{if .LogOutput}}
stdout_log="/var/log/{{.Name}}.out"
stderr_log="/var/log/{{.Name}}.err"
{{end}}
if [ -f "/etc/sysconfig/{{.Name}}" ]; then
set -a
source /etc/sysconfig/{{.Name}}
set +a
fi
exec {{if and .UserName (not .HasSetUIDStanza)}}sudo -E -u {{.UserName}} {{end}}{{.Path}}{{range .Arguments}} {{.|cmd}}{{end}}{{if .LogOutput}} >> $stdout_log 2>> $stderr_log{{end}}
end script
`