xpanta
By:
xpanta

gunicorn gives me "invalid argument" error and cannot be reloaded.

June 9, 2017 439 views
Nginx Ubuntu 16.04

On my droplet I have installed and configured a django website successfully. My client asked me to re-do the project (it was a very old project). So I created another django website on this droplet for testing purposes (for my client to see the new version of the old site before deleting the old one and the new one going live) and added a new domain entry (i.e. stage.example.com) pointing to this droplet.

I edited gunicorn.service file and added this section:

[Service]
User=myuser
Group=www-data
WorkingDirectory=/home/myuser/project2
ExecStart=/home/myuser/.virtualenvs/project2env/bin/gunicorn --workers 3 --bind unix:/home/myuser/project2/project2.sock project2.wsgi:application

Question 1: Is this allowed? To add two [Service] sections in gunicorn.service file?

Anyway, I tried to reload gunicorn and got this error message:

Failed to restart gunicorn.service: Unit gunicorn.service is not loaded properly: Invalid argument.
See system logs and 'systemctl status gunicorn.service' for details.

I opened /var/log/syslog and I noticed this:

Jun  9 08:57:59 project1-fra1 systemd[1]: Reloading.
Jun  9 08:58:00 project1-fra1 systemd[1]: gunicorn.service: Service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.
Jun  9 08:58:00 project1-fra1 systemd[1]: apt-daily.timer: Adding 11h 23.852822s random time.
Jun  9 08:58:00 project1-fra1 systemd[1]: snapd.refresh.timer: Adding 4h 8min 5.201022s random time.
Jun  9 08:58:00 project1-fra1 systemd[1]: Started ACPI event daemon.
Jun  9 09:00:22 project1-fra1 systemd[1]: Stopping A high performance web server and a reverse proxy server...
Jun  9 09:00:22 project1-fra1 systemd[1]: Stopped A high performance web server and a reverse proxy server.
Jun  9 09:00:22 project1-fra1 systemd[1]: Starting A high performance web server and a reverse proxy server...
Jun  9 09:00:22 project1-fra1 systemd[1]: nginx.service: Failed to read PID from file /run/nginx.pid: Invalid argument
Jun  9 09:00:22 project1-fra1 systemd[1]: Started A high performance web server and a reverse proxy server.

I removed the second section from gunicorn.service file and tried to reload the gunicorn service, but although the site is working as normal, I still receive the same "Invalid Argument" error message.

Question 2: What did I do wrong? Is this fixable? How?

3 Answers
hansen June 9, 2017
Accepted Answer

Hi @xpanta

There cannot be multiple [Service] sections in one project (there are special cases, where it's allowed).
You need different projects, since they need in their own environment and have access to their own socket files, etc.

There might be orphaned processes because of the wrong configuration.
You can have a look at all processes running with this command:

sudo ps -aux

Restarting the server will also clear any orphaned processes.

The seconds question is an Nginx warning/error. So can you check it's configuration:

sudo service nginx configtest

Otherwise post it's error log:

tail -30 /var/log/nginx/error.log

@xpanta

When you make changes to a service file controlled by systemd (using systemctl), you'll need to do a reload on the daemon using:

systemctl daemon-reload

That'll ensure the new service file is used. It should prompt you to do that automatically when you try to restart/reload/start/stop a service using systemctl, though in some cases it may not.

From there, I'd make sure the service is stopped:

systemctl stop gunicorn

Then try starting it again:

systemctl start gunicorn

If stopping and starting doesn't work, check to see if there are any stray processes and kill them off. Sometimes this can happen when there's issues with the service file configuration.

The issue with NGINX failing to read the process ID from the nginx.pid file is most likely due to failed service, or the service file is setting one PID file, while NGINX is trying to read from another.

@xpanta Starting a new thread, since it was getting a little narrow.

I've never tried to run multiple Gunicorn with SystemD like you're trying.
Try using the default Gunicorn handler:
https://scottlinux.com/2014/04/03/how-to-host-multiple-django-or-python-apps-on-the-same-host-with-nginx-and-gunicorn/
Otherwise you need to create a different gunicorn.service for each project. And you could call them gunicorn-app1.service and gunicorn-app2.service.

Have another answer? Share your knowledge.