Solved Any way to start script on service start/stop?

Peter2121

Active Member

Reaction score: 27
Messages: 142

Hello,
I know, I can use start_precmd macro in rc.d script of a service to launch something before service startup.
But is there any way to do it directly in rc.conf, so this configuration persists after an upgrade of the service package?
Ideally, I would like to configure both pre-start and pre-stop scripts for a service - to track his state with an external program.
Any ideas?
 

Zirias

Son of Beastie

Reaction score: 1,702
Messages: 2,870

There's an extension point at /etc/rc.conf.d/<service_name>. As it's sourced as a shell script, I assume you could even add a start_precmd there (but didn't try myself).
 

SirDice

Administrator
Staff member
Administrator
Moderator

Reaction score: 12,755
Messages: 39,353

Have a separate start script for that other service. Then you can use REQUIRE:, see rcorder(8).
 
OP
P

Peter2121

Active Member

Reaction score: 27
Messages: 142

There's an extension point at /etc/rc.conf.d/<service_name>. As it's sourced as a shell script, I assume you could even add a start_precmd there (but didn't try myself).
Interesting, I'll try it.
 
OP
P

Peter2121

Active Member

Reaction score: 27
Messages: 142

Have a separate start script for that other service. Then you can use REQUIRE:, see rcorder(8).
Yes, but it does not solve the problem of stop_precmd as when I stop this service it would not stop the other it depends on. Sure, I can stop my new service and it will stop the main one, but it is not exactly what I would like to do.
 

SirDice

Administrator
Staff member
Administrator
Moderator

Reaction score: 12,755
Messages: 39,353

Then create your own rc.d script, that won't get overwritten when you update/upgrade the package that has the original rc(8) script.
 

Zirias

Son of Beastie

Reaction score: 1,702
Messages: 2,870

Then create your own rc.d script, that won't get overwritten when you update/upgrade the package that has the original rc(8) script.
If you really need to launch another service before the service in question, I confirm this is the "correct" solution. If you just have to execute some commands, /etc/rc.conf.d might be the more appropriate solution. So it really depends on the usecase.
 
OP
P

Peter2121

Active Member

Reaction score: 27
Messages: 142

If you really need to launch another service before the service in question, I confirm this is the "correct" solution. If you just have to execute some commands, /etc/rc.conf.d might be the more appropriate solution. So it really depends on the usecase.
Yes, I need just execute some commands on start/stop service, so /etc/rc.conf.d solution seems to be more appropriate one.
The problem is that for the moment I cannot determine if my script put into /etc/rc.conf.d is called on service start or on service stop...
 

Zirias

Son of Beastie

Reaction score: 1,702
Messages: 2,870

Yes, I need just execute some commands on start/stop service, so /etc/rc.conf.d solution seems to be more appropriate one.
The problem is that for the moment I cannot determine if my script put into /etc/rc.conf.d is called on service start or on service stop...
It's always sourced, for any command. The intended usage is for additional configuration variables, see rc.conf(5). So, adding commands there is kind of a misuse, but should work.

You can probably add some debug statements there (see rc.subr(8)) and enable rc_debug in /etc/rc.conf to see what's going on.
 
OP
P

Peter2121

Active Member

Reaction score: 27
Messages: 142

I've found an interesting thing: it is impossible to use both start_precmd and start_postcmd in the same startup script. If the script has already start_precmd configured - the start_postcmd is silently ignored! It is not related to /etc/rc.conf.d, the situation is the same if I add start_postcmd directly into the startup script. I don't see any information about this restriction in docs.
Looks like a bug?
NOTE: I use sshd for my tests
EDIT: The problem is really sshd-related, there is no problem with another rc.d scripts. /etc/rc.conf.d approach works perfectly - I can redefine stop_precmd and start_postcmd here.
 
Top