3

Is there a good solution to start, stop, enable or disable a systemd service directly from within my c++ application?

For example: I want to start the /etc/systemd/system/ntp.service if a specific event occurs that is registered by my c++ application.

A solution for this would be to use a bash/shell script that runs systemctl start ntp.service . This script is called from my c++ application via syscall. But I think there should be a better solution for this.

I've searched the web for other questions like this but I only found hints to the sdbus libraries. But I haven't found any tutorial on how to start a .service via sdbus. Is it possible to start a service with sdbus? If yes, is there a good tutorial or manual for it?

F.Cz
  • 37
  • 3
  • I marked your question as similar to https://stackoverflow.com/questions/13843093/start-systemd-service-from-c-c-application-or-call-a-d-bus-service. I don't think you will find a complete answer there, but some ideas on how to start. Maybe You'll be able to complete the answers – ErniBrown Aug 28 '18 at 08:41
  • 1
    Check out the “Invoking a Method, from C, with sd-bus” section from [this sd-bus blog post](http://0pointer.de/blog/the-new-sd-bus-api-of-systemd.html) – the example method used is systemd’s `StartUnit` method, which is exactly the one you need, just with a different argument. – Lucas Werkmeister Aug 28 '18 at 11:52

0 Answers0