仅当通过 SSH 连接到计算机时,用户 systemd 服务才会重新启动
我遇到了一个奇怪的情况,在 Debian 实例上托管的 Web 服务有时会停止,并且不会自动重新启动。但是,当通过 SSH 连接到计算机时,该服务似乎会自动重新启动。
我原本希望服务始终启动并重新启动,你能帮我找出问题所在吗?我可能误解了 systemctl --user 服务的运行方式。
有问题的服务是一个与乘客独立运行的 Rails 应用程序,但我相信问题可能只是 systemd 文件中的配置
。
# .config/systemd/user/my_service.service
[Unit]
Description=passenger with rails server for my_service (production)
After=syslog.target network.target
[Service]
Type=forking
PrivateTmp=yes
WorkingDirectory=/websites/xxx/current
PIDFile=/websites/xxx/shared/tmp/pids/passenger.8080.pid
ExecStart=/home/outscale/.asdf/shims/bundle exec passenger start /websites/xxx/current
ExecStop=/home/outscale/.asdf/shims/bundle exec passenger stop /websites/xxx/current
MemoryAccounting=true
MemoryLimit=3584M
Restart=always
RestartSec=1
TimeoutStopSec=30
KillMode=mixed
StandardInput=null
SyslogIdentifier=%p
# Environment
Environment="RAILS_ENV=production"
Environment="NODE_ENV=production"
[Install]
WantedBy=default.target
systemctl --user daemon-reload
systemctl --user enable my_service
错误 使用其他东西,比如 systemctl --global enable unit ?我希望我的服务与安装该服务的“超大规模”用户一起运行(否则我的版本管理器 asdf 无法按预期工作)
I have a strange situation with a web service hosted on a debian instance, that sometimes stops, and does not restart automatically. However, when SSH-ing into the machine, the service seems to restart automatically.
I originally wanted the service to always be up and restart, could you help me figure out what's wrong ? I may have misunderstood how systemctl --user
services are meant to run.
The service in question is a Rails application running with passenger standalone, but I believe the problem might just be a misconfiguration in the systemd file.
My systemd file
# .config/systemd/user/my_service.service
[Unit]
Description=passenger with rails server for my_service (production)
After=syslog.target network.target
[Service]
Type=forking
PrivateTmp=yes
WorkingDirectory=/websites/xxx/current
PIDFile=/websites/xxx/shared/tmp/pids/passenger.8080.pid
ExecStart=/home/outscale/.asdf/shims/bundle exec passenger start /websites/xxx/current
ExecStop=/home/outscale/.asdf/shims/bundle exec passenger stop /websites/xxx/current
MemoryAccounting=true
MemoryLimit=3584M
Restart=always
RestartSec=1
TimeoutStopSec=30
KillMode=mixed
StandardInput=null
SyslogIdentifier=%p
# Environment
Environment="RAILS_ENV=production"
Environment="NODE_ENV=production"
[Install]
WantedBy=default.target
I have copied this installed the service using
systemctl --user daemon-reload
systemctl --user enable my_service
Was I meant to use something else, like systemctl --global enable unit
? I want my service to run with the "outscale" user installing the service (otherwise my version manager asdf does not work as expected)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我那里找到了问题的解决方案。我误解了 --user 标志的行为(VS 使用服务文件中的 User= 属性)
我在 debian 11 下运行,并且如上述答案中所述,我的服务在注销 ssh 后不一定会关闭,但仅在某些时候(不清楚是否发生在我的服务第一次崩溃或某种垃圾收集时)
并且当在实例中进行 SSH 作为对用户登录的反应并启动所有服务时,服务会神奇地再次启动服务。
因此,修复方法是使用 User= 重新实现服务,并且不使用 --user 标志,使其成为全局可用的服务。
I found the solution to my problem there. I had misunderstood the behavior of the --user flag (VS using the User= property in the service file)
I was running under debian 11 and as stated in the mentioned answer, my service would not necessarily shut down after logging out of ssh, but only at some point (not clear if it happened when my service crashed for the first time or some sort of garbage collection)
And the service would boot up again magically when SSHing in the instance as a reaction to a user login and starting all the services.
So the fix was to reimplement the services using User= and without the --user flag to make it a globally available service.