0

I have application FastAPI which I run directly through below command

gunicorn app.main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0

It works fine. Now I serve this app through systemd service

[Unit]
Description=To run FX FastAPI backend through gunicorn
After=network.target

[Service]
User=root
Group=root
WorkingDirectory=/home/fx/fx_demo_all_modules/fastapi/
Environment="PATH=/home/fx/fx_demo_all_modules/fastapi/venv/bin"
ExecStart=/home/fx/fx_demo_all_modules/fastapi/venv/bin/gunicorn app.main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0


[Install]
WantedBy=multi-user.target

Location of service file is /etc/systemd/system and also I change the owner from root to fx

The service is also worked fine.

Problem comes when I run bash script from python

manufacturer = subprocess.check_output(['/var/www/get-manufacturer.sh'])

The Script is below

#! /bin/bash
var=$(sudo dmidecode -s system-manufacturer)
echo $var

If I run the application direct then it works fine if I run from service it didn't work. Below is journalctl log

/var/www/get-manufacturer.sh: line 2: sudo: command not found

Advance thanks for the solution.

Salman Mushtaq
  • 341
  • 4
  • 23
  • 2
    Try to change `sudo` to `/usr/bin/sudo` and `dmidecode` to `/usr/sbin/dmidecode` – Ivan Sep 12 '22 at 11:36
  • 1
    try the full path to sudo `/usr/bin/sudo` (run `which sudo` and return that output to check if that is the problem). – bert Sep 12 '22 at 11:38

1 Answers1

0

Change the line Environment="PATH=/home/fx/fx_demo_all_modules/fastapi/venv/bin" to below: Environment="PATH=/home/fx/fx_demo_all_modules/fastapi/venv/bin:/sbin:/bin:/usr/sbin:/usr/bin"

Niraj Nandane
  • 1,318
  • 1
  • 13
  • 24
  • Also for gunicorn based app, it is good to add your own gunicorn enviroment details as below: `EnvironmentFile=/etc/my-app/conf-gunicorn.py` conf-gunicorn.py could looks like below: # The number of worker processes for handling requests workers = 8 # The socket to bind bind = "localhost:" # Write access and error info to /var/log accesslog = errorlog = "/var/log/my-app/gunicorn.log" capture_output = True – Niraj Nandane Sep 14 '22 at 04:32