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
nanoto create the file. You will need to usesudoto create and edit this file because it needs to be in the system's service directory.sudo nano /etc/systemd/system/uvicorn.serviceEdit 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_usernamewith the user that should run the service. - Replace
your_usergroupwith the appropriate group, often the same as the username. - Replace
/path/to/your/appwith the path to the directory containing yourmain.pyfile.
- Replace
Reload systemd to recognize the new service:
bashsudo systemctl daemon-reloadEnable the service to start on boot:
bashsudo systemctl enable uvicorn.serviceStart the service immediately:
bashsudo systemctl start uvicorn.serviceCheck 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.