Service gunicorn not starting code=exited, status=203/EXEC - python

I am trying to configure a gunicorn service on an Red hat EC2 vm of amazon.
I created the sercvice file, but when I run it and check the status it tells me that it failed:
[Unit]
Description=Gunicorn instance for a simple hello world app
After=network.target
[Service]
User=ec2-user
Group=nginx
WorkingDirectory=/home/ec2-user/webserverflask
Environment="PATH=/home/ec2-user/webserverflask/venv/bin"
ExecStart=/home/ec2-user/webserverflask/venv/bin/gunicorn --workers 3
--bind unix:webserverflask.sock -m 007 wsgi
Restart=always
[Install]
WantedBy=multi-user.target
The error message:
● webserver.service - Gunicorn instance for a simple hello world app
Loaded: loaded (/etc/systemd/system/webserver.service; enabled; vendor
preset: disabled) Active: failed (Result: exit-code) since Wed
2022-07-06 19:31:08 UTC; 20h ago Main PID: 25957 (code=exited,
status=203/EXEC)
Jul 06 19:31:08 ip-172-31-95-13.ec2.internal systemd[1]:
webserver.service: Main process exited, code=exited, status=203/EXEC
Jul 06 19:31:08 ip-172-31-95-13.ec2.internal systemd[1]:
webserver.service: Failed with result 'exit-code'. Jul 06 19:31:08
ip-172-31-95-13.ec2.internal systemd[1]: webserver.service: Service
RestartSec=100ms expired, scheduling restart. Jul 06 19:31:08
ip-172-31-95-13.ec2.internal systemd[1]: webserver.service: Scheduled
restart job, restart counter is at 5. Jul 06 19:31:08
ip-172-31-95-13.ec2.internal systemd[1]: Stopped Gunicorn instance for
a simple hello world app. Jul 06 19:31:08 ip-172-31-95-13.ec2.internal
systemd[1]: webserver.service: Start request repeated too quickly. Jul
06 19:31:08 ip-172-31-95-13.ec2.internal systemd[1]:
webserver.service: Failed with result 'exit-code'. Jul 06 19:31:08
ip-172-31-95-13.ec2.internal systemd[1]: Failed to start Gunicorn
instance for a simple hello world app.
and here is my wsgi:
from app import app as application
if __name__ == "__main__":
app.run()
and flask app:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == "__main__":
app.run()

I would start debugging by trying to run the ExecStart command manually, and see if that works (or what error you get):
$ cd /home/ec2-user/webserverflask
$ /home/ec2-user/webserverflask/venv/bin/gunicorn --workers 3 --bind unix:webserverflask.sock -m 007 wsgi

I managed to solve the issue.
If anyone had the same issue while trying to deploy a flask gunicorn using the tutorials on internet, here is my answer:
The problem was that the gunicorn file wasn't accessible, I still don't know why but I managed to fix the issue by moving gunicorn to /usr/local/bin/gunicorn
So my service file look like this now:
[Unit]
Description=Gunicorn instance for a simple hello world app
After=network.target
[Service]
User=ec2-user
Group=nginx
ExecStart=/usr/local/bin/gunicorn --workers 3 --chdir /home/ec2-user --bind unix :webserverflask.sock -m 007 webserverflask.wsgi
[Install]
WantedBy=multi-user.target

Related

systemd service keep giving me error when start or get status

I have a python application and I need it to be run as a service, I tried many methods and I was advised to make it as systemd service
I searched and tried some code
here is my unit code
[Unit]
Description=Airnotifier Service
After=network.target
[Service]
Type=idle
Restart=on-failure
User=root
ExecStart=python3 /home/airnotifier/airnotifier/app.py
[Install]
WantedBy=multi-user.target
and then I run the following commands
sudo systemctl daemon-reload
sudo systemctl enable airnotifier.service
sudo systemctl start airnotifier.service
sudo systemctl status airnotifier.service
the service does not run and I am getting this errors
airnotifier#airnotifier:~$ sudo systemctl status airnotifier.service
● airnotifier.service - Airnotifier Service
Loaded: loaded (/lib/systemd/system/airnotifier.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2023-01-09 14:07:38 UTC; 1s ago
Process: 2072 ExecStart=/usr/bin/python3 /home/airnotifier/airnotifier/app.py (code=exited, status=1/FAILURE)
Main PID: 2072 (code=exited, status=1/FAILURE)
Jan 09 14:07:38 airnotifier systemd[1]: airnotifier.service: Scheduled restart job, restart counter is at 5.
Jan 09 14:07:38 airnotifier systemd[1]: Stopped Airnotifier Service.
Jan 09 14:07:38 airnotifier systemd[1]: airnotifier.service: Start request repeated too quickly.
Jan 09 14:07:38 airnotifier systemd[1]: airnotifier.service: Failed with result 'exit-code'.
Jan 09 14:07:38 airnotifier systemd[1]: Failed to start Airnotifier Service.
This is the code that works with me
[Unit]
Description=Airnotifier Service
[Install]
WantedBy=multi-user.target
[Service]
Type=simple
WorkingDirectory=/home/airnotifier/airnotifier
ExecStart=python3 app.py
Restart=always

Failed to start gunicorn.service: Job for gunicorn.socket failed . Ubunto 18.04

I am following this How To Set Up Django with Postgres, Nginx, and Gunicorn on Ubuntu 18.04 guide.
I have created the following file .socket
sudo nano /etc/systemd/system/gunicorn.socket
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
I have created the following file .service. How I have formatted my own version
sudo nano /etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=guilhermenog
Group=www-data
WorkingDirectory=/home/guilhermenog/projetoagenda
ExecStart=/home/guilhermenog/projetoagenda/venv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
agenda.wsgi:application
[Install]
WantedBy=multi-user.target
Than I have tried to execute the following code
sudo systemctl start gunicorn.socket
And i have recibed this message error
Job for gunicorn.socket failed
See "systemctl status gunicorn.socket" and "journalctl -xe" for details.
After i tried the recommended code
● gunicorn.socket - gunicorn socket
Loaded: loaded (/etc/systemd/system/gunicorn.socket; enabled; vendor preset: enabled)
Active: inactive (dead)
Listen: /run/gunicorn.sock (Stream)
Apr 26 18:41:45 projetoagenda systemd[1]: gunicorn.socket: Socket service gunicorn.service not
loaded, refusing.
Apr 26 18:41:45 projetoagenda systemd[1]: Failed to listen on gunicorn socket.
Apr 26 18:48:41 projetoagenda systemd[1]: gunicorn.socket: Socket service gunicorn.service not
loaded, refusing.
Apr 26 18:48:41 projetoagenda systemd[1]: Failed to listen on gunicorn socket.
Apr 26 18:52:53 projetoagenda systemd[1]: gunicorn.socket: Socket service gunicorn.service not
loaded, refusing.
Apr 26 18:52:53 projetoagenda systemd[1]: Failed to listen on gunicorn socket.
Apr 26 18:53:27 projetoagenda systemd[1]: gunicorn.socket: Socket service gunicorn.service not
loaded, refusing.
Apr 26 18:53:27 projetoagenda systemd[1]: Failed to listen on gunicorn socket.
Apr 26 19:02:09 projetoagenda systemd[1]: gunicorn.socket: Socket service gunicorn.service not
loaded, refusing.
Apr 26 19:02:09 projetoagenda systemd[1]: Failed to listen on gunicorn socket.
i have followed this sugetions but nothing happen
Failed to start gunicorn.service: Unit gunicorn.service not found. Ubunto 18.04
sorry for this question, it´s my firts time asking help in english.
I had the same issue and it was also returning the same error. I managed to fix it for myself so I decided to share what I did, perhaps someone finds it useful too:
Fix: First I ran command: sudo systemctl enable gunicorn.socket (it created symlink)
Then I tried running: sudo systemctl start gunicorn.socket - but it returned that the job for gunicorn socket failed, however, I ran sudo systemctl enable gunicorn.socket again and then sudo systemctl start gunicorn.socket and the issue was no more and upon checking status ( command sudo systemctl status gunicorn.socket) it finally returned that it is active (listening).

run Gunicorn with WSGI inside a folder

I have my project structure as below. I use folders to put all the settings files in them.
~/myproject/
- env
- server
- api
- home
- settings
- dev.py
- prod.py
- wsgi
- dev.py
- prod.py
the myproject/server/home/wsgi/dev.py is:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "home.settings.dev")
application = get_wsgi_application()
also inside myproject/server/home/settings/dev.py is:
WSGI_APPLICATION = 'home.wsgi.dev.application'
with all the above setup the server runs perfectly. When i try to deploy and run the gunicorn, it just fails. Here is my gunicorn.service:
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=demouser
Group=www-data
WorkingDirectory=/home/demouser/myproject
ExecStart=/home/demouser/myproject/env/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/demouser/myproject.sock home.wsgi.dev:application
[Install]
WantedBy=multi-user.target
I am not sure why i get this error as the gunicorn never starts:
Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sun 2020-08-30 05:04:48 UTC; 14min ago
Process: 13354 ExecStart=/home/demouser/myproject/env/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/
Main PID: 13354 (code=exited, status=203/EXEC)
Aug 30 05:04:48 localhost systemd[1]: Started gunicorn daemon.
Aug 30 05:04:48 localhost systemd[13354]: gunicorn.service: Failed to execute command: No such file or directory
Aug 30 05:04:48 localhost systemd[13354]: gunicorn.service: Failed at step EXEC spawning /home/demouser/myproject/env/
Aug 30 05:04:48 localhost systemd[1]: gunicorn.service: Main process exited, code=exited, status=203/EXEC
Aug 30 05:04:48 localhost systemd[1]: gunicorn.service: Failed with result 'exit-code'.
~
i hope i made things clear for everyone. thanks
I solved it and here is how if you are interested:
I have installed Gunicorn3 and then i changed the gunicorn.service file and edited these lines :
WorkingDirectory=/home/demouser/myproject/server
ExecStart=/usr/bin/gunicorn3 --access-logfile - --workers 3 --bind unix:/home/demouser/myproject.sock home.wsgi.dev:application

'Main process exited' error Gunicorn systemd file

I follow this article to deploy my Django project. I created gunicorn.service file in /etc/systemd/system/gunicorn.service with this configuration:
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=azizbek
Group=www-data
WorkingDirectory=/home/admin/respositories/ninersComingSoon
ExecStart=/root/.local/share/virtualenvs/ninersComingSoon-_UZsUc5R/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/admin/repositories/ninersComingSoon/niners.sock ninersComingSoon.wsgi:application
[Install]
WantedBy=multi-user.target
Location of my project is /home/admin/respositories/ninersComingSoon
And when I run
systemctl start gunicorn
systemctl enable gunicorn
it must create niners.sock file inside the project directory but it doesn't.
Then I typed this command to figure out what I did wrong.
journalctl -u gunicorn
And the result was
Dec 05 02:05:26 server.niners.uz systemd[1]: Started gunicorn daemon.
Dec 05 02:05:26 server.niners.uz systemd[1]: gunicorn.service: Main process exited, code=exited, status=203/EXEC
Dec 05 02:05:26 server.niners.uz systemd[1]: gunicorn.service: Unit entered failed state.
Dec 05 02:05:26 server.niners.uz systemd[1]: gunicorn.service: Failed with result 'exit-code'.
So can you help me to solve this problem?
The problem was in WorkingDirectory. There was an incorrect path. There should be .../repositories/... instead of .../respositories/...

SystemD python task in Ubuntu 16

I am trying to execute a python3 with systemd in a ubuntu 16.
Following are the configs
[Unit]
Description=email notification server
After=multi-user.target
[Service]
WorkingDirectory=/home/ubuntu/email-noti
Restart=on-failure
ExecStart=/usr/lib/python3 /home/ubuntu/email-noti/email_reader.py
[Install]
WantedBy=multi-user.target
*please note that email_reader.py import a config file and few python3 files from /home/ubuntu/email-noti/
but it always end with following error
● email-noti.service - email notification server
Loaded: loaded (/etc/systemd/system/email-noti.service; enabled; vendor preset: enabled)
Active: inactive (dead) (Result: exit-code) since Tue 2017-03-07 18:00:38 UTC; 6s ago
Process: 11859 ExecStart=/usr/lib/python3 /home/ubuntu/email-noti/email_reader.py (code=exited, status=203/EXEC)
Main PID: 11859 (code=exited, status=203/EXEC)
Mar 07 18:00:38 ip-172-31-24-115 systemd[1]: email-noti.service: Service hold-off time over, scheduling restart.
Mar 07 18:00:38 ip-172-31-24-115 systemd[1]: Stopped email notification server.
Mar 07 18:00:38 ip-172-31-24-115 systemd[1]: email-noti.service: Start request repeated too quickly.
Mar 07 18:00:38 ip-172-31-24-115 systemd[1]: Failed to start email notification server.
but when i manually execute the email_reader.py works totally fine.
Any help appreciated

Categories

Resources