Skip to content

Instantly share code, notes, and snippets.

@petomalina
Created May 27, 2015 18:03
Show Gist options
  • Save petomalina/3e975fc800336e1675e8 to your computer and use it in GitHub Desktop.
Save petomalina/3e975fc800336e1675e8 to your computer and use it in GitHub Desktop.
Example call of service from go-systemd
package bus
/*
Libraries: go-systemd, godbus
Files: unit file in your systemd services location (.service)
*/
import (
"fmt"
"github.com/coreos/go-systemd/dbus"
)
// StartService starts given service by name
func StartService(name string) (string, error) {
bus, err := dbus.New()
if err != nil {
return "", err
}
defer bus.Close()
busChan := make(chan string)
id, err := bus.StartUnit("some-service.service", "fail", busChan) // rename your service
if err != nil {
return "", err
}
status := <-busChan
fmt.Println("Received return status id:", id, "status:", status)
return status, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment