{"id":207566,"date":"2025-07-08T11:02:49","date_gmt":"2025-07-08T03:02:49","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207566\/"},"modified":"2025-07-08T11:02:49","modified_gmt":"2025-07-08T03:02:49","slug":"%e4%bd%bf%e7%94%a8-go-%e7%ae%a1%e7%90%86-systemd-%e6%9c%8d%e5%8a%a1","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207566\/","title":{"rendered":"\u4f7f\u7528 Go \u7ba1\u7406 systemd \u670d\u52a1"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u4f7f\u7528 Go \u7ba1\u7406 systemd \u670d\u52a1<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-23 17:27:29<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u76ee\u524d\u4e0a\u5df2\u7ecf\u6709\u5f88\u591a\u5173\u4e8e<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u7684\u6587\u7ae0\u4e86\uff0c\u81ea\u5df1\u5728\u521d\u6b21\u9605\u8bfb\u8fd9\u4e9b\u6587\u7ae0\u4e2d\uff0c\u4e5f\u89c1\u8bc6\u5230\u4e86\u5f88\u591a\u5b66\u4e60\u601d\u8def\uff1b\u90a3\u4e48\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4f7f\u7528 Go \u7ba1\u7406 systemd \u670d\u52a1\u300b<\/span>\uff0c\u4e5f\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff0c\u5982\u679c\u9605\u8bfb\u5b8c\u540e\u771f\u7684\u5bf9\u4f60\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u6709\u5e2e\u52a9\uff0c\u6b22\u8fce\u52a8\u52a8\u624b\u6307\uff0c\u8bc4\u8bba\u7559\u8a00\u5e76\u5206\u4eab~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u9664\u4e86 os.exec \u8c03\u7528\u67d0\u4e9b\u5185\u5bb9\u4e4b\u5916\uff0c\u6807\u51c6\u5e93\u4e2d\u662f\u5426\u8fd8\u6709\u7528\u4e8e\u7ba1\u7406 systemd \u670d\u52a1\u7684\u5185\u5bb9\uff1f\u6211\u89c1\u8fc7\u4e00\u4e9b\u4e0e dbus \u6302\u94a9\u6216\u4e0e systemd \u96c6\u6210\u7684\u7b2c 3 \u65b9\u5e93\uff0c\u4f46\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u6211\u8bd5\u56fe\u5c3d\u53ef\u80fd\u5730\u5229\u7528\u6807\u51c6\u5e93\u3002<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u6ca1\u6709\u6807\u51c6\u5e93\uff0c\u4f46\u4e5f\u8bb8\u8fd9\u5f88\u6709\u7528<\/p>\n<pre>https:\/\/github.com\/coreos\/go-systemd<\/pre>\n<p>\u6211\u5df2\u7ecf\u6d4b\u8bd5\u8fc7\u5b83\u53ea\u5217\u51fa\u6240\u6709\u5355\u4f4d\u3002<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n\n    \"github.com\/coreos\/go-systemd\/dbus\"\n)\n\nfunc main() {\n\n    systemdConnection, _ := dbus.NewSystemdConnection()\n\n    listOfUnits, _ := systemdConnection.ListUnits()\n\n    for _, unit := range listOfUnits {\n        fmt.Println(unit.Name)\n    }\n\n    systemdConnection.Close()\n}\n\n\n<\/pre>\n<p>\u4e0d\uff0c\u6807\u51c6\u5e93\u4e2d\u6ca1\u6709\u7279\u5b9a\u4e8e systemd \u7684 api\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u4f7f\u7528 Go \u7ba1\u7406 systemd \u670d\u52a1\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207566","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207566","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207566"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207566\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}