0

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).

Amos Shapira
  • 839
  • 2
  • 11
  • 23
  • 2
    Does this answer your question? [Modify systemd unit file without altering upstream unit file](https://serverfault.com/questions/840996/modify-systemd-unit-file-without-altering-upstream-unit-file) – Bob Feb 03 '21 at 13:47
  • 1
    @HermanB yes it looks like the second option in the accepted answer does what I need. The service change is done in batch (as part of AMI creation with Packer), so the "manual" steps make more sense in my context instead of using the interactive `systemctl edit`. But it's good to learn. – Amos Shapira Feb 03 '21 at 22:00

1 Answers1

2

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

Amos Shapira
  • 839
  • 2
  • 11
  • 23
drookie
  • 8,051
  • 1
  • 17
  • 27