Search code examples
javascriptnode.jssystemddbus

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


I want to control systemd services and daemons through dbus in nodejs

each time I want to connect to the system bus and run a command the program hangs in an infinite loop

I used 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)
})

Solution

  • The documentation says getInterface takes four parameters:

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

    You appear to have missed the interfaceName parameter. Looking at the interfaces available at that serviceName and objectPath it seems that you want 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 -                -                                        -
    

    So it should look like this:

    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)
    })
    

    The list of methods on that interface are:

    $ 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    -                -                                        -