Services: svc#

svc (also systemctl) starts, stops and restarts long-running apps, restarts them when they fail, starts enabled ones at boot and keeps their logs.

Services are system-wide and run as root, so starting, stopping, enabling, creating and editing them takes sudo. Anyone can list them, see their status and read their logs.

svc                              # every service: state, PID, uptime
sudo svc start hello             # an example service that ships with Lepton
svc status hello                 # state, PID, restarts and the last log lines
svc logs hello -f                # follow its output (also: journalctl -u hello -f)
sudo svc stop hello              # stops it and every process it started
sudo svc new clock 'while true; do date; sleep 60; done'
sudo svc enable --now clock      # start now and at every boot

Commands#

CommandWhat it does
svc / svc listevery service with its state, PID and uptime
svc status NAMEdetails and the last 10 log lines
svc start / stop / restart NAME…control services
svc enable / disable [--now] NAMEstart at boot (--now also starts or stops it)
svc logs NAME [-n N] [-f]the log; -f follows it until C-c
svc cat NAMEshow the unit file
svc new NAME COMMAND…create ~/.config/lepton/services/NAME.service (root's: /root/.config/…)
svc edit NAMEedit a unit in le; editing a shipped unit makes your own copy
svc is-active / is-enabled NAMEfor scripts: exit status 0 means yes
journalctl -u NAME [-n N] [-f]the same log, systemd-style

Unit files#

A service is a unit file in systemd syntax, named NAME.service, in ~/.config/lepton/services/ (yours; it persists on an installed system) or /etc/lepton/services/ (shipped with Lepton). Yours override a shipped unit with the same name. ExecStart is any lsh command line.

[Unit]
Description=Static web server for ~/public (port 8080)

[Service]
ExecStart=mkdir -p ~/public && httpd -p 8080 ~/public
Restart=on-failure      # no (default) | on-failure | always
RestartSec=2            # seconds before a restart (default 1)
Environment=GREETING=hi "NAME=a b"
WorkingDirectory=~      # default: your home
TimeoutStopSec=5        # wait this long before killing on stop

Other keys are ignored, and svc status lists them.

How services behave#

  • Each run is a Lepton process tree under PID 1, so it shows in ps. Stopping a service ends the whole tree (a terminate signal, then a kill after TimeoutStopSec).
  • A clean exit leaves the service inactive; a non-zero exit or a kill marks it failed, and Restart= decides whether it comes back. The restarts stop after 5 starts in 10 seconds, as in systemd; a manual svc start resets that.
  • Output goes to /var/lib/lepton/log/NAME.log (rotated at 1 MiB), so on an installed system the log survives reboots.
  • Enabled services start at boot, once the data partition is mounted; the list is kept in /var/lib/lepton/services. Every service is stopped cleanly before power-off and reboot.

Shipped services#

  • hello: says hello every two seconds. The tutorial uses it.
  • httpd: serves ~/public on port 8080 with httpd.

httpd#

httpd [-p PORT] [DIR] serves a directory over HTTP (static files only), using the web server built into Erlang/OTP. It makes a good first service:

sudo mkdir -p /root/public && echo 'hello' | sudo tee /root/public/index.html
sudo svc start httpd
curl http://localhost:8080/

Limits#

Only simple services: no dependencies between services (After=, Requires=), timers, socket activation or resource limits. Everything runs as root.

Commands on this page#

CommandWhat it doesUsage
svcstart, stop and restart long-running services (like systemctl)svc [list|status|start|stop|restart|enable|disable|logs|cat|new|edit] ...
journalctlshow service logs (journalctl -u NAME [-n N] [-f])journalctl -u NAME [-n N] [-f]
httpdserve a directory over HTTP (static files)httpd [-p PORT] [DIR]