3

So I wrote this simple systemd service script:-

[Unit]
Description=uwsgi server script

[Service]
User=web
Group=web
WorkingDirectory=/var/www/prod/myproject/releases/current
ExecStart=/bin/bash -c 'source ~/.bash_profile; workon myproject; uwsgi --ini /var/www/prod/myproject/releases/current/myproject/uwsgi_prod.ini'

[Install]
WantedBy=multi-user.target

which works fine - it starts up and I can see my uwsgi processes in htop.

However, it inexplicably shuts down after being idle for 5 minutes.

If I start this process manually in bash console by executing, as web user:-

source ~/.bash_profile
workon myproject
uwsgi --ini /var/www/prod/myproject/releases/current/myproject/uwsgi_prod.ini

my process does not die after being idle.

What could the problem be?

Calvin Cheng
  • 1,136
  • 4
  • 14
  • 18

2 Answers2

2

Solved it after chatting with another dev on systemd channel.

Adding this into the [Service] section solved the problem:-

Type=forking
PIDFile=/tmp/project.pid-3030

The reason is simple. If Type isn't declared, the default is Type=simple, which expects the process does NOT fork so when bash exits, systemd thinks the process is dead and tears down the cgroup.

Jeff Widman
  • 2,465
  • 4
  • 24
  • 20
Calvin Cheng
  • 1,136
  • 4
  • 14
  • 18
1

As you noted, you need to specify the Type otherwise systemd defaults to Type=simple.

However, the official uWSGI docs on managing uWSGI with systemd use Type=notify rather than Type=forking because then there's no need to specify the PIDFile.

Jeff Widman
  • 2,465
  • 4
  • 24
  • 20