In our embedded device we have a main app and several services. Both main app and services are managed by systemd.

The main app has a plugin system, its plugins may be enabled or disabled at runtime. Say we have PluginA and PluginB. Both these plugins require a service, let's call it ServiceP.

Now if we had just one plugin we would start ServiceP during load and stop the service during unload. But as we have two plugins and their lifetimes can overlap, we need some dynamic dependencies or reference counter:

PluginA loaded   --> start ServiceP
PluginB loaded   --> do nothing (already started by PluginA)
PluginA unloaded --> do nothing (still required by PluginB)
PluginB unloaded --> stop ServiceP

Is there a way how to do this with systemd?

I assume not, as systemd manages whole applications. So if we implement the reference counter ourselves, could we have something as this:

# case 1
App starts (don't start ServiceP, app didn't request it yet)
App says it needs ServiceP --> start ServiceP
App says it no longer needs ServiceP --> stop ServiceP

# case 2
App says it needs ServiceP --> start ServiceP
App stops/crashes --> stop ServiceP

I assume it would be quite easy to achieve this by adding/removing dependencies in unit files and reloading them. But when App crashes it would not stop ServiceP, so I need to perform a cleanup action in ExecStopPost=. This means calling systemctl daemon-reload from unit file, which does not seem right. Is there a better solution?

  • Are the plugins distinct systemd services themselves? – jdizzle Mar 21 '19 at 15:13
  • @jdizzle no, they are shared libraries loaded by the main application during runtime – Adam Trhon Mar 21 '19 at 21:41
  • can you run systemctl start from inside your application? – jdizzle Mar 22 '19 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.