0

I have a system service, foo that is started and stopped via /usr/sbin/service restart foo. It in turn appears to be controlled by a shell script /etc/init.d/foo

How can I create a "pre-start" hook, so that I can run an extra shell script prior to this service starting? In this case, the pre-start hook is extra config that has to be fetched from a cloud provider metadata catalog, and then jacked into a configuration file necessary for foo to start properly.

I have considered modifying /etc/init.d/foo directly, which would work. But which would complicate expected frequent patch-level upgrades which I will catch by apt-get upgrade. I want to avoid a solution that requires re-establishing the hook.

A second option is that I could create a fooWrapper service, remove foo from all run levels, and then just start/stop fooWrapper. The implementation of that script would just be my secret sauce + invoking /etc/init.d/foo. The trouble with that is again package upgrades, whether foo would re-insert itself into the various runlevels, and I would then end up running two conflicting copies.

FrobberOfBits
  • 17,634
  • 4
  • 52
  • 86

1 Answers1

1

Your setup suggests that you use sysv init and not yet systemd. If this is the case, read on. Otherwise ignore this answer.

In general, you will have a link like S20foo in /etc/rc.d/rc3.d. The 20 and 3 may be different for you. Normally, you would create a script /etc/init.d.pre_foo that gets your extra config and link it to /etc/rc.d/rc3.d/S19pre_foo. This will start pre_foo before foo.

Ljm Dullaart
  • 4,273
  • 2
  • 14
  • 31
  • That sounds like the right answer, and a very useful approach. Just to clarify, is what makes this work the fact that S19 < S20 and I have an (implicit) guarantee that the scripts will always execute in that order? – FrobberOfBits Feb 02 '18 at 19:59
  • Also, it looks like if I did `service restart foo` using this approach, it would *not* re-run the pre-hook. Right? – FrobberOfBits Feb 02 '18 at 20:07
  • Yes both are correct. And also: be aware that this is not systemd. – Ljm Dullaart Feb 02 '18 at 20:21