Araneida · 笔记

systemd 里跑一个常驻服务:从 unit 文件到日志

2026-08-30 · 日志

手写的服务放到 systemd 下面托管,比 nohup 加脚本重启省心得多。下面是一个最小可用的 unit,放在 /etc/systemd/system/ 下,名字和文件里的服务名保持一致。

[Unit]
Description=log-watcher
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=nobody
ExecStart=/usr/local/bin/log-watcher -c /etc/log-watcher.toml
Restart=on-failure
RestartSec=5
StandardOutput=journal

[Install]
WantedBy=multi-user.target

几个容易踩的点:

日志直接进 journal 就够了,查的时候 journalctl -u log-watcher -f,要看历史 journalctl -u log-watcher --since "2 hours ago"。如果程序自己有日志文件,就把 StandardOutput 关掉,免得两边都写。

还有一点:以 User=nobody 运行时,服务能读到的文件很有限。证书、密钥这类文件记得把 权限和属组调对,不然启动就报权限错误,而 systemd 只会告诉你退出码,看不出原因。

← 返回笔记列表