Running uvicorn as systemd service
See original GitHub issueHello,
I would like to start uvicorn
at startup with systemd
services but for some reason the process exits with an error status=1/FAILURE
. This is how my service file looks like:
Description=Uvicorn systemd service.
After=network.target
StartLimitIntervalSec=0
[Service]
Type=notify
ExecStart=/home/username/env/bin/uvicorn myproject.asgi:application --port 8000 --host 11.202.96.136
User=username
Group=username
RuntimeDirectory=/var/run/uvicorn
WorkingDirectory=/var/www/myproject
ExecReload=/bin/kill -s HUP $MAINPID
KillMode=mixed
TimeoutStopSec=5
PrivateTmp=true
RestartSec=1
Restart=always
[Install]
WantedBy=multi-user.target
and the status:
root@ubuntu-2cpu-4gb-de-fra1:/lib/systemd/system# systemctl status uvicorn.service
● uvicorn.service - Uvicorn systemd service.
Loaded: loaded (/lib/systemd/system/uvicorn.service; enabled; vendor preset: enabled)
Active: activating (auto-restart) (Result: exit-code) since Tue 2020-05-19 10:18:25 UTC; 326ms ago
Process: 847 ExecStart=/home/username/env/bin/uvicorn myproject.asgi:application --port 8000 --host 11.202.96.136 (code=exited, status=1/FAILURE)
Main PID: 847 (code=exited, status=1/FAILURE)
May 19 10:18:25 ubuntu-2cpu-4gb-de-fra1 systemd[1]: Failed to start Uvicorn systemd service..
What could be the reason behind this error? do you think using supervisor
is a better option?
Thank you
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Running FastAPI applications in production
That means that Gunicorn manages workers and Uvicorn processes the requests. We can then package everything up as a standard systemd service ......
Read more >Deployment - Uvicorn
Run uvicorn --reload from the command line for local development. ... INTEGER Maximum number of requests to service before terminating the process.
Read more >deploy gunicorn and uvicorn using systemd
Systemd environment files and declarations don't use shell syntax. It just supports some limited quoting and expansion.
Read more >Systemd Setup for FastAPI Webhook Listener - Majornetwork
In this post I'll show the configurations to run the API using systemd. Let's get into it, on my Debian 10 system.
Read more >Deployments Concepts - FastAPI
When you set up a remote server (a cloud server, a virtual machine, etc.) the simplest thing you can do is to run...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Should anyone else turn up here clueless like me, I ended up with the following, where “jingus” is the ficticious name of my project.
/srv/jingus.example.com/conf/jingus.service
_
, hence_.asgi:application
;$RUNTIME_DIRECTORY
in the unit file never seemed to work so I just wrote everything as an absolute path;<...>/src/env/
is a vanilla python virtualenv - heck to poetry, pipenv, and all other such nonsense;<...>/secrets/env
is a poorly named environment variables file suitable for use with systemd, with0600
permissions - to use its values for management, tryenv $(sudo cat ../secrets/env) your_script_that_needs_them
;[Unit]
- of course the correct thing to do is to have the project load and serve a “gimme a moment” page while it waits for the database but that’s not what I did;From there I ran
sudo systemctl enable /srv/jingus.example.com/conf/jingus.service
Respectfully I dislike the suggestion to use some chat program instead of issues. I’m here two years later wading through this process myself and there’s nary a mention of systemd in the official docs, nor is the chat program anywhere near useful for this, given it has no search function and is for chatting.
The example given by the good Kinzowa, along with the fix suggested by the good lullis, however, are at the end of a very simple google search.