2

This is not about ExecStartPre

One use case when tweaking services is to test a config file before restarting the service.

for example.

$ vim /etc/nginx.conf
$ nginx -t && restart_nginx

the equivalent with systemd is to still having to remember the check before issuing a restart.

Is there something that prevents that very-likely human error? something like ExecRestartStopPre? That is, something it will execute before the stop step of a restart?

gcb
  • 301

2 Answers2

1

I wonder if you should be using ExecReload in this case? Add something like this to your service unit:

ExecReload=bash -c 'nginx -t && nginx -s reload'

This will only send the reload (HUP) signal to nginx if the configuration check passes. Unlike restart, this doesn't perform a stop or start on the service.

Instead of running running systemctl restart nginx, you would run systemctl reload nginx.

larsks
  • 47,453
1

Forgetting a step like syntax checking a server config file, implies that configuration's testing and deployment could be automated more consistently.

If doing the check as a part of systemd service verbs is so very unnatural, do it somewhere else.

Such as when the config file is installed. For example, Ansible copy or template modules have a validate parameter, which runs a command on a temporary file. On a failure return code, the file is never copied into place. The older, presumably working, configuration is kept.

Fortunately, systemd service configuration is very modular. You can have your ExecReload in a drop in file /etc/systemd/system/nginx.service.d/override.conf where it will survive updates to the base unit. Updates from maintainers who might not prioritize double-checking the user's work at the price of increased complexity of the service.

John Mahowald
  • 36,071