@mh-cbon/systemd-simple-api

Systemd simple api to manage services

Usage no npm install needed!

<script type="module">
  import mhCbonSystemdSimpleApi from 'https://cdn.skypack.dev/@mh-cbon/systemd-simple-api';
</script>

README

systemd-simple-api

Simple api for node to interface with systemd bin.

Install

npm i @mh-cbon/systemd-simple-api --save

Usage

var SystemdSimpleApi = require('@mh-cbon/systemd-simple-api');
var sds = new SystemdSimpleApi(/* version */);

// enable sudo
sds.enableElevation(''); // will now read the password from yasudo env variable
sds.enableElevation('the password'); // will now use 'the password'
sds.enableElevation(false); // will disable use of sudo

// systemctl --user -t service --all
sds.list(opts={user: true, t: 'service', all: true}, function (err, items) {
  console.log(items);
})

// systemctl -t timers
sds.list(opts={t: 'timers'}, function (err, items) {
  console.log(items);
})

// systemctl list-unit-files --user
sds.listUnitFiles(opts={user: true}, function (err, items) {
  console.log(items);
})

// systemctl show serviceId --user
sds.describe('serviceId', opts={user: true}, function (err, info) {
  console.log(info);
})

// systemctl start serviceId --user
sds.start('serviceId', opts={user: true}, function (err) {
  console.log(err);
})

// systemctl stop serviceId --user
sds.stop('serviceId', opts={user: true}, function (err) {
  console.log(err);
})

// systemctl reload serviceId --user
sds.reload(opts={user: true}, function (err) {
  console.log(err);
})

// systemctl reload-or-restart serviceId --user
sds.reloadOrRestart(opts={user: true}, function (err) {
  console.log(err);
})

// systemctl daemon-reload
sds.refresh(function () {
  console.log('systemd is refreshed')
})

Install a service

// per user
var service = {
  install:{},
  unit: {
    Description: "your service"
  },
  service: {
    ExecStart: '/bin/sh ...'
  }
}
sds.install({user: true, id: 'fake', properties: service}, done)

// system wide
var service = {
  install:{},
  unit: {
    Description: "your service"
  },
  service: {
    ExecStart: '/bin/sh ...'
  }
}
sds.install({user: !true, id: 'fake', properties: service}, done)


// later...
sds.uninstall({user: !true, id: 'fake', properties: service}, done)

Run the tests

  • install vagrant
  • run npm run test-fedora

Read more