如何使用 systemd 或 supervisor 管理 Twisted 服务的启动、停止和重启?
解读
国内线上环境普遍用 CentOS 7/8、Ubuntu 20.04+、麒麟 V10 等系统,生产要求“开机自启、异常自动拉起、平滑重启、日志可审计”。Twisted 官方只给了一个 twistd 命令,没有自带守护托管,因此面试时考官想确认:
- 你是否理解 Linux 守护进程模型(PID 文件、信号、退出码);
- 能否把 twistd 的“前台模式”无缝嫁接到 systemd/supervisor;
- 是否知道国内云主机“只认 systemd”与容器“supervisor 友好”的差异;
- 有没有踩过“twistd 默认 fork 两次导致 systemd 误判主进程”的坑。
答不到“主进程类型=notify、PIDFile=、KillMode=control-group”这些关键词,基本会被判为“只写过 Demo”。
知识点
-
twistd 三种运行形态
twistd -y service.tac默认后台二次 fork,写 pidfile;twistd -y service.tac --nodaemon保持前台,日志打 stdout/stderr;twistd -y service.tac --syslog把日志送进 syslog,由 rsyslogd 分拣。
-
systemd 核心字段
Type=notify:twistd 内置 sd_notify 支持,启动后主动报告 READY=1,systemd 不再盲等;PIDFile=:告诉 systemd 主进程 pid 位置,与ExecReload=/bin/kill -HUP $MAINPID配合实现平滑重载;KillMode=control-group:确保子进程(线程池、自定义 spawnProcess)一并被杀,避免僵尸;Restart=on-failure:ExitCode≠0 立即重启,防止“一闪而过”的静默故障;StandardOutput=journal+console:国内阿里云/腾讯云默认开 journal,日志直接进journalctl -u xxx,无需额外文件。
-
supervisor 核心字段
nodaemon=true必须加,否则 supervisor 会误判“进程已死”;stdout_logfile_maxbytes=50MB与backupCount=10防止 Twisted 长连接打爆磁盘;stopsignal=INT:Twisted 对 SIGINT 做 graceful shutdown,SIGTERM 可能丢连接;user=twisted:国内等保 2.0 要求“最小权限”,禁止 root 跑业务进程。
-
国内特有细节
- CentOS 7 默认 systemd 219,不支持
Type=exec,必须回退Type=simple; - Ubuntu 22.04 启用了 cgroup v2,systemd 的
MemoryMax=可以硬限制 Twisted 线程池 OOM; - 华为欧拉、麒麟 V10 的 SELinux 策略禁止非标准路径写 pidfile,需
chcon -t systemd_unit_file_t; - 阿里云 SAE、腾讯云 TKE 侧车日志收集只认 stdout,推荐前台模式+console 日志。
- CentOS 7 默认 systemd 219,不支持
答案
一、systemd 方案(CentOS 8/Ubuntu 20.04 实测)
-
创建专用用户
useradd -r -s /bin/false twisted mkdir -p /var/lib/twisted /var/log/twisted chown twisted:twisted /var/lib/twisted /var/log/twisted -
准备 tac 文件
/srv/app/service.tac,确保最外层有from twisted.application import service, internet application = service.Application("demo") internet.TCPServer(8080, factory).setServiceParent(application) -
编写
/etc/systemd/system/twisted-demo.service[Unit] Description=Twisted Demo Service After=network.target [Service] Type=notify NotifyAccess=main PIDFile=/var/lib/twisted/twisted.pid WorkingDirectory=/srv/app ExecStart=/usr/local/bin/twistd \ --pidfile=/var/lib/twisted/twisted.pid \ --logfile=/var/log/twisted/twisted.log \ --nodaemon -y service.tac ExecReload=/bin/kill -HUP $MAINPID KillMode=control-group Restart=on-failure RestartSec=5s User=twisted Group=twisted # 国内云主机小内存场景保护 MemoryMax=512M TasksMax=2048 [Install] WantedBy=multi-user.target -
加载并验证
systemctl daemon-reload systemctl enable --now twisted-demo systemctl status twisted-demo journalctl -u twisted-demo -f停止:systemctl stop twisted-demo
重启:systemctl restart twisted-demo
平滑重载配置:systemctl reload twisted-demo(twistd 收到 SIGHUP 后会重新打开日志文件,业务代码可在 tac 里监听 SIGHUP 做热更新)。
二、supervisor 方案(Ubuntu 18.04 遗留机、容器环境)
-
安装
apt install supervisor -
创建
/etc/supervisor/conf.d/twisted-demo.conf[program:twisted-demo] command=/usr/local/bin/twistd --nodaemon -y /srv/app/service.tac directory=/srv/app user=twisted stdout_logfile=/var/log/supervisor/twisted-demo.out stderr_logfile=/var/log/supervisor/twisted-demo.err stdout_logfile_maxbytes=50MB backupCount=10 stopsignal=INT stopwaitsecs=10 autorestart=true startretries=3 environment=PYTHONUNBUFFERED="1" -
更新并启停
supervisorctl reread supervisorctl update supervisorctl start twisted-demo supervisorctl restart twisted-demo supervisorctl stop twisted-demo
三、面试加分话术
“线上 200 个 Twisted 节点,我用 systemd 的 notify 模式把启动时间从 5 秒降到 1.2 秒,配合 HUP 信号实现日志切割,不需要 USR1 自定义;supervisor 方案留给 Docker 侧车,镜像里只有 supervisord 一个入口,K8s livenessProbe 直接检测 8080 端口,既满足等保又通过公司 SRE 评审。”
拓展思考
- 如果 Twisted 服务需要监听 80 端口但禁止以 root 启动,可结合 systemd 的
AmbientCapabilities=CAP_NET_BIND_SERVICE或 setcap 提权,面试可展开“Capabilities vs setuid”优劣。 - 当 tac 文件里启动 3 个子进程(例如 SSH、WebSocket、DNS)时,systemd 的
KillMode=mixed可以只杀主进程而保留子进程,适合“热升级父进程”的灰度策略。 - 国内金融级合规要求“关机前必须优雅等待 30 秒”,可在 systemd 单元里加
TimeoutStopSec=30并在代码里捕获 SIGTERM 做reactor.callLater(25, reactor.stop),保证连接全部关闭。 - 容器场景下,supervisor 与 s6、runit 的对比:supervisor 占用 20 MB 内存,s6 仅 2 MB,面试可谈“边缘网关 128 MB 设备选 s6,云主机充裕用 supervisor”。