ubuntugunicornsystemctl

gunicorn.service: Failed at step CHDIR spawning */env/bin/gunicorn: No such file or directory


gunicorn.service cannot open WorkingDirectory and the gunicorn executable. I think it's about permissions but I don't know how I can solve it.

sudo systemctl status gunicorn:

Mar 15 12:12:42 ns1.localhost.com systemd[1]: Started gunicorn daemon.
Mar 15 12:12:42 ns1.localhost.com systemd[16439]: gunicorn.service: Changing to the requested working directory failed: No such file or directory
Mar 15 12:12:42 ns1.localhost.com systemd[16439]: gunicorn.service: Failed at step CHDIR spawning /home/radarkes-api/env/bin/gunicorn: No such file or directory
Mar 15 12:12:42 ns1.localhost.com systemd[1]: gunicorn.service: Main process exited, code=exited, status=200/CHDIR
Mar 15 12:12:42 ns1.localhost.com systemd[1]: gunicorn.service: Failed with result 'exit-code'.

/etc/systemd/system/gunicorn.service:

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/home/radarkes-api/src
ExecStart=/home/radarkes-api/env/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/radarkes-api/src/core.sock core.wsgi:application

[Install]
WantedBy=multi-user.target

I can run /home/radarkes-api/env/bin/gunicorn manually, this is the output which means I can access the gunicorn executable:

usage: gunicorn [OPTIONS] [APP_MODULE]
gunicorn: error: No application module specified.

I tried:

tree -d -L 2 /home/radarkes-api/:

radarkes-api/
├── env
│   ├── bin
│   └── lib
└── src
    ├── api
    └── core

How can I solve this?


Solution

  • ExecStart must be a sub-path of WorkingDirectory

    in your example /etc/systemd/system/gunicorn.service will look like this:

    [Unit]
    Description=gunicorn daemon
    After=network.target
    
    [Service]
    User=root
    Group=www-data
    WorkingDirectory=/home/radarkes-api/src
    ExecStart=/home/radarkes-api/src/env/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/radarkes-api/src/core.sock core.wsgi:application
    
    [Install]
    WantedBy=multi-user.target
    

    PS:

    1. In your case, it's preferable to create a new virtual env instead of moving your existing env folder inside of src one (to avoid paths issues),
    2. Make sure all your pip3 requirements are available in your requirements.txt file,
    3. Make sure your .gitignore includes the new virtual env folder

    Then execute:

    $ cd /home/radarkes-api/src
    $ python3 -m venv MyEnvName
    $ echo 'MyEnvName/' >> .gitignore
    $ pip3 install -r requirements.txt