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:

  1. 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 use sudo to create and edit this file because it needs to be in the system's service directory.


    sudo nano /etc/systemd/system/uvicorn.service
  2. 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 your main.py file.
  3. Reload systemd to recognize the new service:

    bash
    sudo systemctl daemon-reload
  4. Enable the service to start on boot:

    bash
    sudo systemctl enable uvicorn.service
  5. Start the service immediately:

    bash
    sudo systemctl start uvicorn.service
  6. Check the status of the service:

    You can check if the service is running correctly with:

    bash
    sudo 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.