start unicorn on Start up Linux Ubuntu
To make the uvicorn main:app --host 0.0.0.0 --port 7111
command start at system boot on a Linux system, you can create a systemd service. Here are the steps:
Create a systemd service file:
Create a new service file for your application. Open a terminal and use a text editor like
nano
to create the file. You will need to usesudo
to create and edit this file because it needs to be in the system's service directory.sudo nano /etc/systemd/system/uvicorn.service
Edit the service file:
Add the following content to the file. Adjust the paths and the user as needed for your specific environment.
ini[Unit] Description=Uvicorn FastAPI app After=network.target [Service] User=your_username Group=your_usergroup WorkingDirectory=/path/to/your/app ExecStart=/usr/bin/env uvicorn main:app --host 0.0.0.0 --port 7111 Restart=always [Install] WantedBy=multi-user.target
- Replace
your_username
with the user that should run the service. - Replace
your_usergroup
with the appropriate group, often the same as the username. - Replace
/path/to/your/app
with the path to the directory containing yourmain.py
file.
- Replace
Reload systemd to recognize the new service:
bashsudo systemctl daemon-reload
Enable the service to start on boot:
bashsudo systemctl enable uvicorn.service
Start the service immediately:
bashsudo systemctl start uvicorn.service
Check the status of the service:
You can check if the service is running correctly with:
bashsudo systemctl status uvicorn.service
This will ensure that your Uvicorn application starts automatically at system boot and runs as a background service. If you need to make any changes to the service file, remember to reload the systemd daemon with sudo systemctl daemon-reload
and restart the service with sudo systemctl restart uvicorn.service
.