Skip to content

Latest commit

 

History

History
62 lines (46 loc) · 1.63 KB

systemd.md

File metadata and controls

62 lines (46 loc) · 1.63 KB

Running workerd with systemd

systemd can open privileged sockets (such as ports 80 and 443) on workerd's behalf while running workerd under an unprivileged user account. workerd inherits sockets from the parent process with --socket-fd.

Socket activation example

Assuming /etc/workerd/config.capnp defines two sockets named http and https:

/etc/systemd/system/workerd.service

[Unit]
Description=workerd runtime
After=local-fs.target remote-fs.target network-online.target
Requires=local-fs.target remote-fs.target workerd.socket
Wants=network-online.target

[Service]
Type=exec
ExecStart=/usr/bin/workerd serve /etc/workerd/config.capnp --socket-fd http=3 --socket-fd https=4
Sockets=workerd.socket

# If workerd crashes, restart it.
Restart=always

# Run under an unprivileged user account.
User=nobody
Group=nogroup

# Hardening measure: Do not allow workerd to run suid-root programs.
NoNewPrivileges=true

[Install]
WantedBy=multi-user.target

/etc/systemd/system/workerd.socket

[Unit]
Description=sockets for workerd
PartOf=workerd.service

[Socket]
ListenStream=0.0.0.0:80
ListenStream=0.0.0.0:443

[Install]
WantedBy=sockets.target

Graceful shutdown

On SIGTERM, workerd stops accepting new connections, lets open requests finish, and exits. In cluster mode it also hands its Durable Objects off to peers within WORKERD_CLUSTER_STOP_TIMEOUT_MS (8 seconds by default), after any WORKERD_SHUTDOWN_DELAY_MS delay. Set TimeoutStopSec above that total (for example TimeoutStopSec=30) so systemd does not send SIGKILL before handoff completes. Run workerd serve --help for all shutdown options.