Search code examples
passengersystemdasdf

User systemd service restarting only when SSH-ing into the machine


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)


Solution

  • 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.