385. Services
A KDOS service is a script in /etc/init.d named NN_name.sh. rcS runs every enabled one at boot, in numeric order, and the long-running daemons among them are kept alive by ksvc, the supervisor. This page covers managing them by hand: listing them and their state, starting, stopping and restarting one, choosing which start at boot, and finding out why one did not. Adding a service of your own is on Writing a service; scheduled jobs are on Timers. Every command on this page runs on a running KDOS machine.
Installation
Services are part of every image. Three things provide them:
- kdos-tools installs the supervisor,
/usr/sbin/ksvc, and the command that manages services,/usr/sbin/service, which is a link to it:
install -Dm755 kdos-tools "$PKG/usr/sbin/ksvc"
install -Dm644 "$PORT_SRC/kglobalaccel/org.kdos.tiling.desktop" \
"$PKG/usr/share/kglobalaccel/org.kdos.tiling.desktop"
install -Dm644 "$PORT_SRC/kglobalaccel/org.kdos.nightlight.desktop" \
"$PKG/usr/share/kglobalaccel/org.kdos.nightlight.desktop"
install -Dm644 "$PORT_SRC/autostart/kdos-mpris.desktop" \
"$PKG/etc/xdg/autostart/kdos-mpris.desktop"
install -Dm644 "$PORT_SRC/autostart/kdos-speech-sync.desktop" \
"$PKG/etc/xdg/autostart/kdos-speech-sync.desktop"
ln -s ksvc "$PKG/usr/sbin/service"
src/system/kdos-tools/build.sh, lines 25–35
The base filesystem,
fs/etc/init.d/in the tree, supplies the scripts every image has, and/etc/init.d/service_helper, the shell functions those scripts call.Ports add scripts of their own from their
build.sh, so a script exists only while its package is installed. rcS and the service scripts in the KDOS book lists both sets, and Services says what each one starts. On a running machine the complete list is:ls /etc/init.d
Which services start at boot differs between the live medium and an installed system:
| Live medium | Installed system | |
|---|---|---|
Markers in /etc/service.disabled |
sshd, from the image |
The live session’s markers, copied with the rest of the root, after which the installer writes or removes one for each of its five service choices. The SSH server is off unless chosen; CUPS is on unless unticked |
| Where a change is kept | In memory, gone at power-off, unless a persistence store exists; see Live persistence | On the root filesystem |
The installer’s five choices, and the services key of an answer file, are in Answer files.
Configuration
/etc/service.disabled/
A service is disabled by an empty file in this directory named after the service, and enabled by removing it. Nothing else records whether a service starts at boot. The image ships one, fs/etc/service.disabled/sshd. The rules are in the configuration reference’s entry for the marker.
The installer writes or removes the markers for the services in its table:
const Service ki_services[] = {
{ "networkmanager", "NetworkManager", "wired, wifi and VPN", 1 },
{ "bluetooth", "Bluetooth", "bluez daemon", 1 },
{ "alsa", "ALSA state", "restore mixer levels at boot", 1 },
{ "cups", "CUPS", "printing daemon", 1 },
{ "sshd", "OpenSSH server", "accepts remote logins", 0 },
};
src/system/kinstall-core/conf.c, lines 29–35
The last field is whether the service is on by default.
/etc/init.d/service_helper
Every supervised script sources this file. Three of its functions are thin wrappers over ksvc:
supervise() {
ksvc supervise "$@"
}
stop_service() {
ksvc stop-supervised "$1"
}
check_status() {
ksvc check "$1"
}
fs/etc/init.d/service_helper, lines 48–58
The fourth, wait_bus_name NAME, returns once NAME has an owner on the system bus, or after ten seconds with [WARN] NAME is not on the system bus after 10 seconds and status 1. These scripts call it after supervise: 40_dbus.sh waits for org.freedesktop.DBus, 40_elogind.sh for org.freedesktop.login1, 41_polkitd.sh for org.freedesktop.PolicyKit1, 42_modemmanager.sh for org.freedesktop.ModemManager1, and 43_settingsd.sh, 44_accounts.sh, 46_sysupdated.sh, 55_powerd.sh and 56_tlppd.sh for theirs. So each of those daemons answers on the bus before the scripts after it start. 42_networkmanager.sh, 45_avahi.sh, 48_iio-sensor-proxy.sh and 60_bluetooth.sh do not wait, so a script after them can start before NetworkManager, avahi, iio-sensor-proxy or bluetoothd has its name on the bus. A daemon ksvc supervises has no D-Bus activation file, so the bus never starts a second copy of it, and a call to its name while it is stopped is refused with ServiceUnknown; see How polkit and ModemManager are started.
A service script
A typical script, 70_sshd.sh. start checks that the daemon exists and starts it under the supervisor in the foreground; stop and status ask the supervisor:
. /etc/init.d/service_helper
NAME="sshd"
DAEMON="/usr/sbin/sshd"
case "$1" in
start)
if [ ! -x "$DAEMON" ]; then
echo "[SKIP] $NAME: $DAEMON not found"
exit 0
fi
echo "[KDOS] Starting $NAME..."
mkdir -p /run/sshd
# -A makes each default host key type this OpenSSH knows that is
# missing and leaves an existing key alone. sshd offers only the types
# it finds a key file for, so a type left ungenerated is one a client
# that insists on it cannot connect with.
ssh-keygen -A
# -D = don't detach (foreground for supervision). PidFile=none:
# the supervisor already knows the daemon's pid and keeps its own
# in /run/kdos-svc.sshd.pid, so a pid file of sshd's is one more
# file to go stale and nothing reads it.
supervise "$NAME" "$DAEMON" -D -o PidFile=none
;;
stop)
stop_service "$NAME"
;;
status)
check_status "$NAME"
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
;;
esac
The service’s name, the one every service command takes, comes from the file name: sshd for 70_sshd.sh. The name the script passes to supervise (its NAME) is the one the supervisor’s files under /run carry, and it can differ: 35_chrony.sh is the service chrony and supervises chronyd, 22_syslog.sh is syslog and supervises syslogd, and 30_network.sh is network and supervises dhcpcd.
Usage
Listing services
List every service, whether it starts at boot, and its current state:
service listEach row is one line: the service name,
[ENABLED]or[DISABLED], and the first line the script’s ownstatusprinted, cut to the terminal’s width. A script that prints more —utmpsa line for each ofutmpdandwtmpd,timersone per job,tlpits whole status report — gets(+N more)after that line, andservice status <name>shows the rest. A script whosestatusprints nothing shows—:modulesanduserdirsalways, andlvmon a machine with no logical volumes.Ask about one service:
service status sshdA supervised service answers
[ OK ] sshd (pid N)or[DOWN] sshd, or[FAIL] sshd (exited with code N; restarting)while its daemon keeps exiting and the supervisor keeps starting it again (see Troubleshooting). A service with no daemon reports what it applied instead; the meaning of each is in Services with no daemon.
Starting, stopping and restarting
Start, stop or restart a service for this boot only:
sudo service start cups sudo service stop cups sudo service restart cupsrestartruns the script’sstop, waits a second and runs itsstart. A supervised daemon is stopped as soon as it exits afterSIGTERM, and killed if it is still running when its stop grace runs out (five seconds unless the script named another).starton a service that is already running starts nothing: it prints[SKIP] <name>: already supervised (pid N).cupstakes its two companions with it:80_cups.shstartscups-browsedandipp-usbaftercupsdand stops them before it, and either one started whilecupsdis not running prints[SKIP] <name>: CUPS is off.A name that matches no script exactly is matched as part of one, so
service status sshreaches70_sshd.sh. Rely on this only forstatus,start,stopandrestart; see the warning under Choosing what starts at boot.The script can also be run directly, which is what
servicedoes:sudo /etc/init.d/80_cups.sh start
Choosing what starts at boot
Find the exact service name in the first column of
service list.Disable or enable it:
sudo service disable bluetooth sudo service enable bluetoothdisablecreates/etc/service.disabled/bluetoothandenableremoves it. Neither starts or stops the service now; do that withstartorstop.Check the marker:
ls /etc/service.disabled
enable and disable accept part of a name too, but write the marker under the name exactly as typed, and the boot looks for the script’s own name, so a shortened name succeeds and changes nothing. Always give the full name.
Turning the SSH server on and off
The SSH server is tied to its firewall rule. An administrator (a member of wheel) opens both at once:
kdos-power firewall ssh on
This removes /etc/service.disabled/sshd, starts 70_sshd.sh and opens the SSH port in the firewall, a tcp dport 22 accept rule in nft list ruleset, and prints ok ssh on; the first start also generates the host keys. off puts the marker back, stops the server, closes the port and prints ok ssh off. See Opening a service.
Turning NetworkManager off
Disable and stop it:
sudo service disable networkmanager sudo service stop networkmanagerStart the fallback DHCP client for this boot; from the next boot on,
30_networkstarts it by itself:sudo service start network
Nothing else starts it again. NetworkManager has no D-Bus activation file, so a call to its name while it is stopped, such as the ones the network applet makes in every desktop session, fails with org.freedesktop.DBus.Error.ServiceUnknown and starts nothing; the marker is the only switch. The same holds for every daemon a service script supervises; see the configuration reference’s entry for the marker.
Tips
ksvc check <name>asks the supervisor directly about the name a script passed tosupervise:ksvc check chronyd, notchrony. Given the service’s name instead it finds no pid file and answers[DOWN] chronywhile the daemon runs. It is also how one periodic job is checked, under the namekdos-timer-<job>.- A supervised daemon’s output is in
/run/kdos-svc.<name>.log, under the supervisor’s name, and in the system log tagged with that name. What its script printed at boot is in/run/kdos-init.<service>.log, under the service name. See Logs. - A service that is enabled but skips itself at boot prints a
[SKIP]line with the reason, for example[SKIP] ngircd: no /etc/ngircd.conf (/usr/share/doc/ngircd/sample-ngircd.conf is the template), and its stage on the boot splash readsSKIPrather thanOK:rcScloses the stage of a script that exits 0 after a line starting[SKIP], and whose log has noSupervisor startedoralready supervisedline fromksvc, withkdos-splash skip. A stage that skipped one part and started another readsOK. A script written this way changes nothing at install time: the service starts once its configuration exists. kdos doctorchecks thatkdos-powerd,kdos-energydandkdos-oomdare running, by their sockets, and warns about each one that is not with theservice startcommand that starts it. On a machine with no RAPL energy counters, most virtual machines among them, it reports[skip] no RAPL energy domain on this machine — per-app energy cannot be measured here at allinstead of a stoppedkdos-energyd, whose script skips it there.kdos restartslists the processes still running files that an upgrade replaced. Restart a daemon among them withsudo service restart <service>, using the service’s name, not the process’s.
Troubleshooting
Error: Service 'x' not found. No /etc/init.d/NN_x.sh exists, and no service name contains x. Check ls -l /etc/init.d: a name that is not two digits, an underscore, a name and .sh is not a service. A port’s script is missing when the port is not installed.
service start x prints nothing and the service is not in service list. The script is not executable. service list, rcS and rcK skip it, and service fails to run it without a message. Run sudo chmod 755 /etc/init.d/NN_x.sh.
A disabled service still starts at boot. The marker has the wrong name. Compare ls /etc/service.disabled with the first column of service list, remove the wrong marker, and run sudo service disable <full name>.
A service is [DOWN] after boot. Read what its script printed:
cat /run/kdos-init.<service>.log
A [SKIP] line gives the reason the script did not start the daemon. The scripts that skip themselves on unsuitable hardware are listed in Services that skip themselves.
The daemon keeps restarting. The supervisor restarts it five seconds after every exit, and service status answers [FAIL] <name> (exited with code N; restarting) between the starts, [FAIL] <name> (exited with code N, M failed starts in a row; restarting) once it has failed more than once in a row, and [FAIL] <name> (crash loop: M failed starts in a row, …) while a daemon that failed twice in a row is running again and has not yet lasted a minute. An exit with a non-zero status counts as a failed start, and so does an exit with status 0 within five seconds of the start. A failure after a minute or more of running starts the count again at one, and an exit with status 0 after five seconds clears it. The supervisor keeps that state in /run/kdos-svc.<name>.state. Its log shows each exit and the daemon’s own last words:
sudo tail -n 40 /run/kdos-svc.<name>.log
Lines of the form [KDOS] (<name>) Exited with code <N>. Restarting in 5s... repeat. Correct what the daemon complains about, then sudo service restart <service>.
[KDOS] (smartd) Exited with code 17, a final status: not restarting. The daemon decided it has nothing to do on this machine, and its script told the supervisor that this exit is final. On a virtual machine smartd ends this way, finding no disk that reports SMART data, and service status smartd then answers [DOWN] smartd. thermald ends the same way on an Intel machine with nothing for it to drive, and on any other processor its script skips it with [SKIP] thermald: not an Intel processor. Nothing needs fixing; disable the service to skip it at boot.
[FAIL] <name> (supervisor gone; daemon pid N still running). The supervisor was killed and its daemon was left running. service start will not start a second copy beside it: it waits for that daemon for the stop grace and then refuses with [FAIL]. Stop it, then start the service again:
sudo service stop <service>
sudo service start <service>
Error: Unknown command '<word>'. The words are in the wrong order. service takes the verb first and the service name second, the name from the script’s file name and not the daemon’s:
sudo service start powerd
Known gaps
- LVM volume groups are activated only at boot:
03_lvmruns once - The live image’s accounts have known passwords; see Security
See also
- Services: every shipped script and what it starts
- ksvc and service: the command’s reference
- Supervision and logging: how the supervisor runs a daemon
- The daemons: KDOS’s own root daemons and their scripts
- Init and runlevels, Writing a service, Timers, Logs, Time and NTP, Out of memory