<!-- skill: Pi Services | version: 1.0.0 | description: Run things 24/7 on Raspberry Pi with systemd — services, timers, logs, hardening | category: System & Productivity -->

## Instructions
- Anything that must survive reboot/login gets a systemd **user service** (`~/.config/systemd/user/`) or system service (`/etc/systemd/system/`) — never `tmux`/`nohup` for production.
- Unit essentials: `Description`, `ExecStart` (absolute paths!), `Restart=on-failure`, `RestartSec=5`, `WantedBy=default.target` (user) or `multi-user.target` (system).
- Environment via `Environment=` lines or `EnvironmentFile=` — never bake secrets into the unit when avoidable; `chmod 600` files holding tokens.
- Logs go to the journal by default: `journalctl --user -u name -f` to watch, `--since today` to slice. Add `StandardOutput=append:/path.log` only for app-specific files.
- Timers (`*.timer` + `OnCalendar=daily`) beat cron for anything needing logging/dependencies/retry.
- Harden public services: bind `127.0.0.1` + reverse proxy, or firewall (`ufw`), fail2ban for SSH, updates via `unattended-upgrades`.

## Examples
- Discord bot 24/7: user service running `python bot.py` in its folder, restarts on crash.
- Nightly backup: timer at 03:00 running a backup script, `OnFailure=` alert unit.

## Workflows
1. Make it run in foreground reliably by hand first.
2. Write the `.service` (absolute paths, env, restart policy).
3. `systemctl --user daemon-reload && enable --now name`, watch logs.
4. Reboot-test: does it come back? (`sudo reboot`, then check).
5. Document: what it is, where logs live, how to restart — in the project notes.

## Templates
```ini
# ~/.config/systemd/user/discordbot.service
[Unit]
Description=My Discord bot
After=network-online.target
[Service]
WorkingDirectory=/home/casper/bot
ExecStart=/usr/bin/python3 bot.py
Restart=on-failure
RestartSec=5
[Install]
WantedBy=default.target
```
```bash
systemctl --user daemon-reload
systemctl --user enable --now discordbot
journalctl --user -u discordbot -f
# enable lingering so user services survive logout:
sudo loginctl enable-linger casper
```

## Tools
- SARAH `shell_run` (non-interactive systemctl/journalctl commands) and `file_write` for unit files.
- `systemd-analyze blame` when boot slows down.

## Documentation
- systemd.service: https://www.freedesktop.org/software/systemd/man/systemd.service.html
- Timers: https://wiki.archlinux.org/title/Systemd/Timers
