I have an old-school daemon that I want to control using systemd. When its configuration file changes, it needs to be killed and restarted. In other words, after editing the config file, systemctl reload MYSERVICE
should kill the process and restart it.
Attempt 1: Try the defaults. This tells systemd how to start the daemon, but not how to reload it.
[Service]
ExecStart=/usr/bin/MYSERVICE
Type=simple
As a result, start
and restart
work, but reload
gives this error:
# systemctl reload MYSERVICE
Failed to reload MYSERVICE.service: Job type reload is not applicable for unit MYSERVICE.service.
Attempt 2: Tell it how to kill the process. This kills the process but systemd doesn't restart it for me.
[Service]
ExecStart=/usr/bin/MYSERVICE
Type=simple
ExecReload=/bin/kill -HUP $MAINPID
…followed by…
# systemctl daemon-reload
# systemctl reload MYSERVICE
…kills the process but it is not restarted automatically.
Attempt 3: Use ExecReload to restart the process too. This fails for a few reasons:
ExecReload=/bin/kill -HUP $MAINPID ; /usr/bin/MYSERVICE
…the error message I get…:
# systemctl daemon-reload
# systemctl reload MYSERVICE
Job for MYSERVICE.service failed because the control process exited with error code. See "systemctl status MYSERVICE.service" and "journalctl -xe" for details.
I would expect there to be a ReloadType=kill_and_restart or something but no such luck.
How to tell systemd to kill and restart a daemon on reload?
Best Answer
The answer is, "you don't"! But we have good news.
systemd's philosophy is that reload is optional and should be left undefined if there is no true reload functionality. I'd define "true reload functionality" as a reload that does not kill and restart the service, or make the service change its PID. In other words, systemd only wants to reflect what features exists.
Instead, you should use
systemctl reload-or-restart
which will do a reload if it exists, and a restart if it does not.From the man page...
Therefore: (1) leave ExecReload blank, (2) use
systemctl reload-or-restart MYSERVICE
and, (3) you should be all set.If you do try to use ExecReload to define a way to kill and restart the service, it will have a new PID and systemd would be confused.