Executing a command before starting a systemd service [duplicate]

I want to execute a script before starting a service every time the service is started.

I know that I can use ExecStartPre inside the service definition file, but I wonder if it's possible to achieve without editing a file which comes from the service's package.

The script is a simple command which exits when it's done, it creates files which are used by the main service (it fetches TLS certificates and keys from secure storage).


Solution 1:

Sure. You can use specific independant includes:

mkdir /etc/systemd/service/<servicename>.service.d
echo /etc/systemd/service/<servicename>.service.d/<anything>.conf << EOF
[Service]
ExecStartPre=<path to script>
EOF
systemctl daemon-reload

et voila.

Reference: Search for "drop-in" in systemd.unit — Unit configuration