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; scope?: 'system' | 'user'; user?: string }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.
await startService({ name: 'nginx.service' });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 running service. No-op if the service is not active.
await restartService({ name: 'nginx.service' });reloadService()
Section titled “reloadService()”Reloads a running service’s configuration. No-op if not active.
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.
const output = await journalRead({ afterCursor: '...', lines: 50 });journalVacuum()
Section titled “journalVacuum()”Cleans up old journal entries by size, time, or file count.
await journalVacuum({ size: '500M', time: '7d' });setHostname()
Section titled “setHostname()”IDEMPOTENT
Sets the system hostname using hostnamectl set-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' });Utility Functions
Section titled “Utility Functions”getUnitPath(name, options?)
Section titled “getUnitPath(name, options?)”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'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 —
sleep.conf(5)types - coredump —
coredump.conf(5)types - sysusers —
sysusers.d(5)parsing/serialization - tmpfiles —
tmpfiles.d(5)parsing/serialization - networkd — systemd-networkd
.link/.network/.netdevtypes