Skip to content

ManualRemote

systemd integration

Run the devserver as a systemd user service on Linux, restart or upgrade it without losing terminals, and report it to systemd-report.

The devserver under systemd

On Linux, chan devserver start runs the devserver as the systemd user service chan-devserver.service, and there a restart or an upgrade keeps every terminal running. Set it up first as in Start and stop it.

Under launchd on macOS, or chan's own daemon on Windows, FreeBSD and Linux without systemd (--service=chan), a restart closes the terminals.

What you can do

  • Manage the service with chan devserver or with systemd's own tools.
  • Restart the devserver, or let systemd restart a hung one, while every terminal keeps running.
  • Upgrade chan and restart into the new release without closing a terminal.
  • Report the devserver's readings to systemd-report.

The devserver service

chan devserverWhat it does to chan-devserver.service
startTurns on linger so the service survives logout, writes the unit to ~/.config/systemd/user/, enables it at boot, starts it, and prints the launch URL and token. On a running service it changes nothing and prints them again.
statusSays whether the service runs, shows the command the unit runs, and prints the launch URL with its token.
restartRewrites the unit for the chan binary you run it with, enables it, and restarts the service, or starts it when it is stopped. Terminals keep running.
stopCloses every terminal, stops the service, and disables it so it does not come back at boot.
$chan devserver status
chan devserver (systemd): running -- chan-devserver.service
  command: /home/user/.local/bin/chan devserver run --bind=127.0.0.1 --port=8787
chan devserver: listening on http://127.0.0.1:8787/?t=Zr8mT2xQwN6vLb3HcY5dKp7sJf9aGe4u

The unit is an ordinary user unit:

$systemctl --user cat chan-devserver
# /home/user/.config/systemd/user/chan-devserver.service
[Unit]
Description=chan devserver
After=network.target

[Service]
Type=notify
NotifyAccess=main
FileDescriptorStoreMax=1024
KillMode=process
Environment="PATH=/home/user/.local/bin:/usr/local/bin:/usr/bin:/bin"
ExecStart=/home/user/.local/bin/chan devserver run --bind=127.0.0.1 --port=8787
TimeoutStartSec=10min
Restart=on-failure
WatchdogSec=30

[Install]
WantedBy=default.target
CommandShows
systemctl --user status chan-devserverThe service's state and its process tree.
journalctl --user -u chan-devserverThe devserver's log.

Restart without losing terminals

Restart the devserver either way:

$chan devserver restart
$systemctl --user restart chan-devserver

Only the devserver process restarts. The shells and their programs keep running with the same process ids, and the devserver rebuilds each terminal with its scrollback and screen.

  • Each terminal reads session ended (parked) while the devserver is away. A restart that takes more than about half a second also greys the window under reconnecting to the chan server, from about a second after the devserver goes away.
  • Chan Desktop windows, and a browser's workspace and standalone terminal windows, then reload by themselves and reattach to the same shells.
  • After chan devserver rotate-token, the next restart gives standalone terminal windows a new token too, as does a restart that rotates a token older than 30 days. A browser tab of one then reads missing or invalid token: open the devserver's Computers screen at the new URL and open the window again from its row. Its terminals are still there.
  • Chan Desktop stays connected over ssh or Eternal Terminal when the devserver is back within 30 seconds.
  • A coding agent mid-turn keeps working, and so does cs in the shells that carried on. A cs tunnel running at the restart ends.
What happensYour terminals
chan devserver restart or systemctl --user restart chan-devserverKeep running.
systemd's watchdog replaces a hung devserver, or a crashed one comes backKeep running.
chan devserver stop or systemctl --user stop chan-devserverEnd: every shell gets a hangup.
chan devserver restart --forceEnd before the restart.
The machine rebootsEnd.

Upgrade a running devserver

A restart starts whatever binary the unit names, so a restart after an upgrade is a live upgrade:

$chan upgrade && chan devserver restart

chan upgrade replaces the binary, and the running devserver stays on the old release until chan devserver restart updates the unit and restarts into the new one. systemctl --user restart chan-devserver starts the new binary but keeps the old unit.

chan came fromUpgrade it with, then run chan devserver restart
The install script, or a .deb or .rpm downloaded from a releasechan upgrade.
The Ubuntu PPA, Fedora COPR, the AUR, or NixThe package manager. chan upgrade refuses there.
Chan Desktopchan upgrade, which updates the desktop in the background. Restart the devserver once the desktop has relaunched.

To check which release runs, see Watch the devserver.

Watch the devserver

The Dashboard's Metrics slide, and chan metrics on the devserver's machine, name the running devserver's version, build, process id and uptime. After a restart the process id and uptime are new and the graphs start over.

$chan metrics

A failed read ends chan metrics --watch, so it rides out a restart only when the devserver answers again within about a second. The flags are in See what it costs.

Report the devserver to systemd-report

systemd-report, in systemd 260 and later, collects metrics from a machine's services (systemd-report(1), the design). Two optional user units make the devserver a source, app.chan.Devserver: systemd runs chan metrics --varlink for each connection.

  1. Write the two units

    The service runs the chan your shell finds, and the block prints its path.

    $mkdir -p ~/.config/systemd/user && cd ~/.config/systemd/user cat > chan-metrics.socket <<'EOF' [Unit] Description=chan devserver metrics for systemd-report [Socket] ListenStream=%t/systemd/report/app.chan.Devserver FileDescriptorName=varlink SocketMode=0600 Accept=yes MaxConnections=16 RemoveOnStop=yes [Install] WantedBy=sockets.target EOF command -v chan cat > chan-metrics@.service <<EOF [Unit] Description=chan devserver metrics for systemd-report [Service] Type=exec ExecStart=$(command -v chan) metrics --varlink StandardInput=socket StandardOutput=socket StandardError=journal NoNewPrivileges=yes EOF cd - >/dev/null

    If the devserver has its own CHAN_HOME, add the same Environment="CHAN_HOME=..." line under the service's [Service].

  2. Enable the socket

    A socket that is already running does not take a changed path until it is restarted.

    $systemctl --user daemon-reload systemctl --user enable chan-metrics.socket && systemctl --user restart chan-metrics.socket
  3. Read the source

    systemd-report is in /usr/lib/systemd/, outside PATH. On systemd 260, use describe-metrics in place of describe.

    $/usr/lib/systemd/systemd-report --user list-sources /usr/lib/systemd/systemd-report --user describe app.chan.Devserver /usr/lib/systemd/systemd-report --user metrics app.chan.Devserver
    FAMILY                                     OBJECT FIELDS                         VALUE
    app.chan.Devserver.Build                   -      -                              "git-95696ef5e38e"
    app.chan.Devserver.CPUUsageNSec            -      -                              30000000
    app.chan.Devserver.HomeLogicalBytes        -      -                              425
    app.chan.Devserver.IOWriteBytes            -      {"scope":"storage"}            122880
    app.chan.Devserver.InstanceId              -      -                              "6dae770e5c7063c80c777d147fcbbedf"
    app.chan.Devserver.MemoryResidentBytes     -      -                              69668864
    app.chan.Devserver.PID                     -      -                              970
    app.chan.Devserver.TrafficReceivedBytes    -      {"layer":"network"}            804
    app.chan.Devserver.TrafficSentBytes        -      {"layer":"network"}            9925
    app.chan.Devserver.TransferJobs            -      {"outcome":"finished"}         0
    app.chan.Devserver.UptimeSec               -      -                              6
    app.chan.Devserver.Version                 -      -                              "0.129.0"
    ...
    
    48 metrics listed.

docs/metrics.md lists every family with its type and unit.

CommandWhat it does
--json=short, added to a systemd-report commandPrints each metric as one JSON object.
varlinkctl --more call "$XDG_RUNTIME_DIR/systemd/report/app.chan.Devserver" io.systemd.Metrics.List '{}'Reads the source without systemd-report. Any Varlink client can.
systemctl --user disable --now chan-metrics.socketTurns the source off.
Details and limits
  • With XDG_CONFIG_HOME set, chan writes the unit to $XDG_CONFIG_HOME/systemd/user/ instead.
  • status prints the launch URL only in a terminal. Through a pipe it prints the address, and --url prints the URL there too.
  • stop leaves the unit file in place, so status still shows its command.
  • Type=notify: the devserver reports ready once it has rebuilt every terminal. A workspace that takes more than a minute to mount does not hold it back, and that workspace's terminals follow.
  • NotifyAccess=main keeps the shells under the devserver from talking to systemd in its name.
  • WatchdogSec=30 with Restart=on-failure: the devserver checks in every 15 seconds, and systemd restarts it when it hangs or crashes.
  • KillMode=process: a restart signals only the devserver process, never the shells.
  • TimeoutStartSec=10min gives a large set of terminals time to rebuild before the devserver reports ready.
  • Environment= carries CHAN_HOME when you set one, and the PATH of the terminal that last ran start or restart, since a user service otherwise starts with the user manager's environment. A run from a script, with no terminal, keeps the PATH already recorded.
  • Each terminal in a window hands its PTY, and a memory file holding its scrollback, to systemd's file descriptor store when it opens. The restarted devserver takes them back and rebuilds each terminal as it stood at the last byte read.
  • Through a restart the shells keep their process ids, the process tree below them and their control group, and your systemd user instance adopts them when the old devserver process exits.
  • FileDescriptorStoreMax=1024 holds 512 terminals, two descriptors each. A terminal opened while 512 are stored runs normally, but a restart ends it.
  • A stop releases the file descriptor store, which is what sends every shell its hangup.
  • The source serves systemd's metrics interface, io.systemd.Metrics, on a socket in $XDG_RUNTIME_DIR/systemd/report/, the directory systemd-report --user reads.
  • From systemd 261, systemd-report also builds the metrics into one JSON report that it can upload to a server of yours, and from 262 it can sign the report.
  • A client of the source needs no token: the source finds the devserver's address and token on every read, so it follows a restart and a rotated token.
  • The families are counters and gauges, not rates: two reports give a rate. chan metrics --json --history has chan's own rates and the last ten minutes.
  • Counters start over when the devserver restarts. app.chan.Devserver.InstanceId names the devserver process, so a reader can tell a restart from a counter that went down.
  • A connection never starts the devserver. While it is down the source reports no metrics, and the report carries on without them.
  • Reports more often than every five minutes keep the devserver scanning the chan home once a minute, as any metrics read does.

Next