使用Go管理systemd服务

huangapple go评论87阅读模式
英文:

Managing systemd services with Go

问题

在标准库中,除了os.exec调用外,是否有其他用于管理systemd服务的内容?我看到了一些第三方库,它们与dbus进行连接或与systemd集成,但我在这种情况下尽量想利用标准库。

英文:

Outside of os.exec calling something, is there anything in the standard library for managing systemd services? I have seen a few 3rd party libraries that hook into dbus or integrate with systemd but I was trying to leverage the standard library as much as possible in this case.

答案1

得分: 1

这是没有标准库,但可能会有用的代码:

https://github.com/coreos/go-systemd

我已经测试过它可以列出所有的单元。

package main

import (
	"fmt"

	"github.com/coreos/go-systemd/dbus"
)

func main() {

	systemdConnection, _ := dbus.NewSystemdConnection()

	listOfUnits, _ := systemdConnection.ListUnits()

	for _, unit := range listOfUnits {
		fmt.Println(unit.Name)
	}

	systemdConnection.Close()
}

请注意,这是一个用于与 systemd 交互的 Go 语言库。

英文:

There is no standard library but maybe this can be useful

https://github.com/coreos/go-systemd

I've tested it to just list all units.

package main

import (
	"fmt"

	"github.com/coreos/go-systemd/dbus"
)

func main() {

	systemdConnection, _ := dbus.NewSystemdConnection()

	listOfUnits, _ := systemdConnection.ListUnits()

	for _, unit := range listOfUnits {
		fmt.Println(unit.Name)
	}

	systemdConnection.Close()
}


答案2

得分: 0

没有,标准库中没有针对systemd的特定API。

英文:

No, there is no systemd-specific API in standard library.

huangapple
  • 本文由 发表于 2021年12月20日 12:58:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/70417531.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定