Skip to content

systemd

Operations for managing systemd services, journal, daemon configuration, and system-level settings like hostname, timezone, and locale.

import {
getServiceInfo,
enableService,
disableService,
startService,
stopService,
restartService,
reloadService,
daemonReload,
journalRead,
journalVacuum,
setHostname,
setTimezone,
setLocale,
getUnitPath,
getSystemdConfigPath,
getSystemdConfigDropInPath,
} from '@sysopkit/linux/systemd';

Most service operations accept ServiceOptions with the following fields:

{ name: string; scope?: 'system' | 'user'; user?: string }
  • scope — Defaults to "system". Use "user" for user-scoped units.
  • user — Username for user-scope (determines home directory paths).

Retrieves service state information from systemctl show.

const info = await getServiceInfo({ name: 'nginx.service' });
// { LoadState: 'loaded', ActiveState: 'active', SubState: 'running',
// UnitFileState: 'enabled', MainPid: 1234 }

IDEMPOTENT

Enables a service to start at boot. Skips if already enabled.

await enableService({ name: 'nginx.service' });

IDEMPOTENT

Disables a service from starting at boot. Skips if already disabled.

await disableService({ name: 'nginx.service' });

IDEMPOTENT

Starts a service. Returns true if the service was started, false if already running.

await startService({ name: 'nginx.service' });

IDEMPOTENT

Stops a running service. No-op if already stopped.

await stopService({ name: 'nginx.service' });

Restarts a running service. No-op if the service is not active.

await restartService({ name: 'nginx.service' });

Reloads a running service’s configuration. No-op if not active.

await reloadService({ name: 'nginx.service' });

Reloads systemd manager configuration. Required after creating, modifying, or deleting unit files.

await daemonReload();
await daemonReload({ scope: 'user' });

Reads journal entries. Supports cursor-based incremental reading.

const output = await journalRead({ afterCursor: '...', lines: 50 });

Cleans up old journal entries by size, time, or file count.

await journalVacuum({ size: '500M', time: '7d' });

IDEMPOTENT

Sets the system hostname using hostnamectl set-hostname. No-op if already set to the desired value.

await setHostname({ name: 'web-01' });

IDEMPOTENT

Sets the system timezone using timedatectl set-timezone. No-op if already set.

await setTimezone({ name: 'America/New_York' });

IDEMPOTENT

Sets a locale variable using localectl set-locale.

await setLocale({ name: 'LANG', value: 'en_US.UTF-8' });

Returns the absolute path to a systemd unit file.

const path = getUnitPath('nginx.service');
// '/etc/systemd/system/nginx.service'
const userPath = getUnitPath('foo.service', { scope: 'user', user: 'alice' });
// '/home/alice/.config/systemd/user/foo.service'

getSystemdConfigPath(configName, options?)

Section titled “getSystemdConfigPath(configName, options?)”

Returns the absolute path to a systemd daemon configuration file.

const path = getSystemdConfigPath('journald.conf');
// '/etc/systemd/journald.conf'

getSystemdConfigDropInPath(configName, dropInName)

Section titled “getSystemdConfigDropInPath(configName, dropInName)”

Returns the absolute path to a drop-in configuration file.

const path = getSystemdConfigDropInPath('journald.conf', 'sysops');
// '/etc/systemd/journald.conf.d/sysops.conf'

The systemd module also exports TypeScript types for configuration files. See the following pages for details:

  • journaldjournald.conf(5) types
  • logindlogind.conf(5) types
  • resolvedresolved.conf(5) types
  • timesyncdtimesyncd.conf(5) types
  • sleepsleep.conf(5) types
  • coredumpcoredump.conf(5) types
  • sysuserssysusers.d(5) parsing/serialization
  • tmpfilestmpfiles.d(5) parsing/serialization
  • networkd — systemd-networkd .link/.network/.netdev types