Rails 7.0.4部署时puma:restart报错服务未找到,疑与--user参数有关
解决Capistrano部署Puma服务重启时的"Unit not found"问题
问题背景
- 环境:Rails 7.0.4、Puma 6.0
- 执行
bundle exec cap production deploy部署生产环境时,卡在puma:restart步骤 - 服务器上
R7-reporting-2022_puma_production.service服务正常运行,但Capistrano用systemctl --user restart ...执行时报错:Unit R7-reporting-2022_puma_production.service not found - 直接在服务器执行
systemctl restart R7-reporting-2022_puma_production.service(不带--user参数)能成功完成重启
修复方案
修改Capistrano的Puma配置,让它使用系统级的systemctl命令,而非用户级的。
操作步骤
打开项目中的
config/deploy.rb或者config/deploy/production.rb配置文件添加或更新以下配置项:
set :puma_systemctl_user, :system这个配置会告诉Capistrano的Puma插件,调用系统级的
systemctl命令,不再附加--user参数。重新执行部署命令
bundle exec cap production deploy,puma:restart步骤就能正常完成。
原因说明
systemctl --user用于管理当前用户专属的systemd服务,这类服务的配置文件通常存放在用户目录下(比如~/.config/systemd/user/)- 你的
R7-reporting-2022_puma_production.service是系统级服务(配置文件在/etc/systemd/system/目录),所以用--user参数找不到对应的服务单元 - 把
puma_systemctl_user设为:system后,Capistrano会执行不带--user的systemctl restart命令,匹配你的服务类型,自然就能找到并重启服务了
内容的提问来源于stack exchange,提问作者prodigerati
相关产品推荐
相关产品推荐

