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';Service Options
Section titled “Service Options”Most service operations accept ServiceOptions with the following fields:
{ name: string | readonly string[]; scope?: 'system' | 'user'; user?: string }name— Single unit or list of units. Multiple units are queued in a singlesystemctlcall (systemd v262EnqueueUnitJobMany).scope— Defaults to"system". Use"user"for user-scoped units.user— Username for user-scope (determines home directory paths).
getServiceInfo()
Section titled “getServiceInfo()”Retrieves service state information from systemctl show.
const info = await getServiceInfo({ name: 'nginx.service' });// { LoadState: 'loaded', ActiveState: 'active', SubState: 'running',// UnitFileState: 'enabled', MainPID: 1234 }enableService()
Section titled “enableService()”IDEMPOTENT
Enables a service to start at boot. Skips if already enabled.
await enableService({ name: 'nginx.service' });disableService()
Section titled “disableService()”IDEMPOTENT
Disables a service from starting at boot. Skips if already disabled.
await disableService({ name: 'nginx.service' });startService()
Section titled “startService()”IDEMPOTENT
Starts a service. Returns true if the service was started, false if already running. Pass an array to start several units with one systemctl call — returns one boolean per unit, in input order.
await startService({ name: 'nginx.service' });await startService({ name: ['a.service', 'b.service'] });// [true, false]stopService()
Section titled “stopService()”IDEMPOTENT
Stops a running service. No-op if already stopped.
await stopService({ name: 'nginx.service' });restartService()
Section titled “restartService()”Restarts a loaded service (systemctl restart; starts it if not running).
await restartService({ name: 'nginx.service' });reloadService()
Section titled “reloadService()”Reloads a loaded service’s configuration (systemctl reload).
await reloadService({ name: 'nginx.service' });daemonReload()
Section titled “daemonReload()”Reloads systemd manager configuration. Required after creating, modifying, or deleting unit files.
await daemonReload();await daemonReload({ scope: 'user' });journalRead()
Section titled “journalRead()”Reads journal entries. Supports cursor-based incremental reading. Output includes a trailing -- cursor: ... footer from --show-cursor.
const output = await journalRead({ afterCursor: '...', lines: 50 });journalVacuum()
Section titled “journalVacuum()”Cleans up old journal entries by size, time, or file count. Throws if no option is given.
await journalVacuum({ size: '500M', time: '7d' });setHostname()
Section titled “setHostname()”IDEMPOTENT
Sets the system hostname using hostnamectl hostname. No-op if already set to the desired value.
await setHostname({ name: 'web-01' });setTimezone()
Section titled “setTimezone()”IDEMPOTENT
Sets the system timezone using timedatectl set-timezone. No-op if already set.
await setTimezone({ name: 'America/New_York' });setLocale()
Section titled “setLocale()”IDEMPOTENT
Sets a locale variable using localectl set-locale.
await setLocale({ name: 'LANG', value: 'en_US.UTF-8' });// bare form sets LANG:await setLocale({ locale: 'C.UTF-8' });Utility Functions
Section titled “Utility Functions”getUnitPath(name, options?)
Section titled “getUnitPath(name, options?)”Returns the path to a systemd unit file. Absolute for system scope and for user scope when user is given (assumes /home/${user}); otherwise relative to the target user’s home directory.
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 daemon drop-in file (e.g., journald.conf.d/; not unit foo.service.d/ drop-ins under /etc/systemd/system/).
const path = getSystemdConfigDropInPath('journald.conf', 'sysops');// '/etc/systemd/journald.conf.d/sysops.conf'Configuration Types
Section titled “Configuration Types”The systemd module also exports TypeScript types for configuration files. See the following pages for details:
- journald —
journald.conf(5)types - logind —
logind.conf(5)types - resolved —
resolved.conf(5)types - timesyncd —
timesyncd.conf(5)types - sleep —
systemd-sleep.conf(5)types - system-conf —
systemd-system.conf(5)types (system.conf/user.conf) - coredump —
coredump.conf(5)types - zram-generator —
zram-generator.conf(5)types - sysusers —
sysusers.d(5)parsing/serialization - tmpfiles —
tmpfiles.d(5)parsing/serialization - networkd — systemd-networkd
.link/.network/.netdevtypes