# How to automatically stop/start a service if another service is stopped/started?



## mrjayviper (Sep 5, 2017)

scenario: I have 3 daemons. daemon-x, daemon-y and daemon-z. daemon-y and daemon-z depend on daemon-x.

2 things that I want.

1. I want daemon-y and daemon-z to only start after daemon-x.
I believe using "REQUIRE" will take care of this during boot time. Please confirm if this is true.

Assuming all 3 services are currently stopped. I want daemon-y and daemon-z to automatically start if I manually start daemon-z.

2. I want daemon-y and daemon-z to automatically stop if I stopped daemon-x.

Any ideas on how this can be done? Thank you


----------



## SirDice (Sep 5, 2017)

mrjayviper said:


> If I believe using "REQUIRE" will take care of this during boot time. Please confirm if this is true.


It doesn't. All REQUIRE and friends do is try to get the order correct. See rcorder(8):

```
BUGS
     The "REQUIRE" keyword is misleading: It doesn't describe which daemons
     have to be running before a script will be started.  It describes which
     scripts must be placed before it in the dependency ordering.  For
     example, if your script has a "REQUIRE" on "named", it means the script
     must be placed after the "named" script in the dependency ordering, not
     necessarily that it requires named(8) to be started or enabled.
```



> Any ideas on how this can be done? Thank you


Probably the only reliable way to do this is to start all three daemons from the same rc(8) script. Because that's the only place that will allow you to find out if things started/stopped correctly.


----------



## mrjayviper (Sep 5, 2017)

SirDice said:


> Probably the only reliable way to do this is to start all three daemons from the same rc(8) script. Because that's the only place that will allow you to find out if things started/stopped correctly.



that's my other alternative. thanks!


----------



## SirDice (Sep 6, 2017)

You can get quite creative with rc(8) scripts. Besides the standard start and stop 'commands' you can add your own custom additions. You could for example create a script when called normally; `service myscript start` simply starts the daemons in the correct order. Then add a 'stopfront' for example; `service myscript stopfront` to only stop Z while keeping X and Y running. You probably already have some ideas what would be needed, and just add them as 'extra' commands.


----------

