A service that nobody can hear, and a stop that lands in the middle — cheat sheet
python, logging-journald
sys.stdout.reconfigure(line_buffering=True) # in the program, once
print("…", flush=True) # per call
# from outside: python3 -u, or PYTHONUNBUFFERED=1 in the unit
stopping = False
def stop(signum, frame):
global stopping; stopping = True
signal.signal(signal.SIGTERM, stop)
signal.signal(signal.SIGINT, stop)
while not stopping:
... # check the flag at a safe point
with open(ledger, "a", encoding="utf-8") as f:
f.write(f"{name},{amount}\n") # one complete record per write
fd, tmp = tempfile.mkstemp(dir=os.path.dirname(state))
… ; os.fsync(f.fileno()) ; os.replace(tmp, state)
journalctl -fu ingest # follow a service's output
systemd-run --wait --pipe -q cmd # run something with a service's stdout
systemctl show unit -p Result -p ExecMainStatus