1

I have a problem running a Gunicorn Web service using Systemd.

Here are the files I created in order to execute:

The shell script file (/home/ubuntu/mata.sh):

#!/usr/bin/env bash

cd /home/ubuntu/workspace/test-api
/home/ubuntu/workspace/mata_venv/bin/gunicorn --workers=4 app:app --bind 0.0.0.0:xxxx

Here's my .service file (/lib/systemd/system/mata.service):

[Unit]
Description=Test API Service
After=multi-user.target
[email protected]

[Service]
User=ubuntu
Type=simple
ExecStart=/home/ubuntu/mata.sh
StandardInput=tty-force

[Install]
WantedBy=multi-user.target

Running the shell script on its own works well, but when running systemctl status mata.service, I am getting the following message:

Started Test API Service
mata.service: Main process exited, code=exited, status=216/GROUP
mata.service: Unit entered failed state.
mata.service: Failed with result 'exit-code'.

Any idea?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
Pavel Zagalsky
  • 293
  • 1
  • 3
  • 9
  • The sequencing here is throwing me off a little bit; are you starting it manually and then running `systemctl status...`? You are starting it with systemctl, right? – Jeff Schaller Feb 19 '19 at 14:38
  • 1
    Possible duplicate of [systemd - My custom service exits with status code 216/GROUP](https://unix.stackexchange.com/questions/374273/systemd-my-custom-service-exits-with-status-code-216-group) – JdeBP Feb 19 '19 at 14:43
  • @JdeBP, I don't see that the user set a `Group=` definition here; is the implication that `User=ubuntu` is failing because of that user's groups? (The other aspect of the duplicate answer relates to using `nobody`, and seems unrelated to me) – Jeff Schaller Feb 19 '19 at 15:03
  • [systemd exit code 216](https://freedesktop.org/software/systemd/man/systemd.exec.html#id-1.20.8) indicates "Failed to determine or change group credentials." – Jeff Schaller Feb 19 '19 at 15:07
  • @JeffSchaller Yep, I am trying to start it with systemctl start mata.service – Pavel Zagalsky Feb 19 '19 at 15:33
  • Is there anything unusual about the output of `groups ubuntu`? – Jeff Schaller Feb 20 '19 at 14:50

1 Answers1

0

You don't have a "Type=simple" service, you have a forking service, since it's not the "mata.sh" process that you care about, it's the "gunicorn" process.

For a Type=simple service, change the [Service] section to:

[Service]
User=ubuntu
Type=simple
WorkingDirectory=/home/ubuntu/workspace/test-api
ExecStart=/home/ubuntu/workspace/mata_venv/bin/gunicorn --workers=4 app:app --bind 0.0.0.0:xxxx
StandardInput=tty-force

... where I imported the cd command with a WorkingDirectory directive.

Or make it a Forking service with:

[Service]
User=ubuntu
Type=forking
ExecStart=/home/ubuntu/mata.sh
StandardInput=tty-force

You may prefer the Type=forking solution if, as it appears, the gunicorn process starts sub-processes (workers=4).

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250