如何通过nodejs中的dbus包启动和停止systemd服务?

How to Start and Stop systemd services through dbus package in nodejs?

我想在nodejs中通过dbus控制systemd服务和守护进程

每次我想连接到系统总线和运行一个命令程序挂在无限循环中

我用了node-dbus

const dbus = require("dbus");

const someBus = dbus.getBus('system');

someBus.getInterface('org.freedesktop.systemd1',
  '/org/freedesktop/systemd1',
  (err, interface) => {
  if (err) throw new Error(err)
  console.log(interface)
})

documentation表示getInterface需要四个参数:

Bus.prototype.getInterface(serviceName, objectPath, interfaceName, callback)

您似乎错过了 interfaceName 参数。查看 serviceNameobjectPath 上可用的接口,您似乎想要 org.freedesktop.systemd1.Manager

$ busctl introspect org.freedesktop.systemd1 /org/freedesktop/systemd1 | grep interface
org.freedesktop.DBus.Introspectable       interface -                -                                        -
org.freedesktop.DBus.Peer                 interface -                -                                        -
org.freedesktop.DBus.Properties           interface -                -                                        -
org.freedesktop.systemd1.Manager          interface -                -                                        -

所以它应该是这样的:

const dbus = require("dbus");

const someBus = dbus.getBus('system');

someBus.getInterface('org.freedesktop.systemd1',
  '/org/freedesktop/systemd1', 'org.freedesktop.systemd1.Manager',
  (err, interface) => {
  if (err) throw new Error(err)
  console.log(interface)
})

该接口上的方法列表是:

$ busctl introspect org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager | grep method
.AbandonScope                             method    s                -                                        -
.AddDependencyUnitFiles                   method    asssbb           a(sss)                                   -
.AttachProcessesToUnit                    method    ssau             -                                        -
.CancelJob                                method    u                -                                        -
.ClearJobs                                method    -                -                                        -
.DisableUnitFiles                         method    asb              a(sss)                                   -
.Dump                                     method    -                s                                        -
.DumpByFileDescriptor                     method    -                h                                        -
.EnableUnitFiles                          method    asbb             ba(sss)                                  -
.Exit                                     method    -                -                                        -
.GetDefaultTarget                         method    -                s                                        -
.GetDynamicUsers                          method    -                a(us)                                    -
.GetJob                                   method    u                o                                        -
.GetJobAfter                              method    u                a(usssoo)                                -
.GetJobBefore                             method    u                a(usssoo)                                -
.GetUnit                                  method    s                o                                        -
.GetUnitByControlGroup                    method    s                o                                        -
.GetUnitByInvocationID                    method    ay               o                                        -
.GetUnitByPID                             method    u                o                                        -
.GetUnitFileLinks                         method    sb               as                                       -
.GetUnitFileState                         method    s                s                                        -
.GetUnitProcesses                         method    s                a(sus)                                   -
.Halt                                     method    -                -                                        -
.KExec                                    method    -                -                                        -
.KillUnit                                 method    ssi              -                                        -
.LinkUnitFiles                            method    asbb             a(sss)                                   -
.ListJobs                                 method    -                a(usssoo)                                -
.ListUnitFiles                            method    -                a(ss)                                    -
.ListUnitFilesByPatterns                  method    asas             a(ss)                                    -
.ListUnits                                method    -                a(ssssssouso)                            -
.ListUnitsByNames                         method    as               a(ssssssouso)                            -
.ListUnitsByPatterns                      method    asas             a(ssssssouso)                            -
.ListUnitsFiltered                        method    as               a(ssssssouso)                            -
.LoadUnit                                 method    s                o                                        -
.LookupDynamicUserByName                  method    s                u                                        -
.LookupDynamicUserByUID                   method    u                s                                        -
.MaskUnitFiles                            method    asbb             a(sss)                                   -
.PowerOff                                 method    -                -                                        -
.PresetAllUnitFiles                       method    sbb              a(sss)                                   -
.PresetUnitFiles                          method    asbb             ba(sss)                                  -
.PresetUnitFilesWithMode                  method    assbb            ba(sss)                                  -
.Reboot                                   method    -                -                                        -
.ReenableUnitFiles                        method    asbb             ba(sss)                                  -
.Reexecute                                method    -                -                                        -
.RefUnit                                  method    s                -                                        -
.Reload                                   method    -                -                                        -
.ReloadOrRestartUnit                      method    ss               o                                        -
.ReloadOrTryRestartUnit                   method    ss               o                                        -
.ReloadUnit                               method    ss               o                                        -
.ResetFailed                              method    -                -                                        -
.ResetFailedUnit                          method    s                -                                        -
.RestartUnit                              method    ss               o                                        -
.RevertUnitFiles                          method    as               a(sss)                                   -
.SetDefaultTarget                         method    sb               a(sss)                                   -
.SetEnvironment                           method    as               -                                        -
.SetExitCode                              method    y                -                                        -
.SetUnitProperties                        method    sba(sv)          -                                        -
.StartTransientUnit                       method    ssa(sv)a(sa(sv)) o                                        -
.StartUnit                                method    ss               o                                        -
.StartUnitReplace                         method    sss              o                                        -
.StopUnit                                 method    ss               o                                        -
.Subscribe                                method    -                -                                        -
.SwitchRoot                               method    ss               -                                        -
.TryRestartUnit                           method    ss               o                                        -
.UnmaskUnitFiles                          method    asb              a(sss)                                   -
.UnrefUnit                                method    s                -                                        -
.UnsetAndSetEnvironment                   method    asas             -                                        -
.UnsetEnvironment                         method    as               -                                        -
.Unsubscribe                              method    -                -                                        -