I am trying to run my FastAPI application by uvicorn and systemd. But it fails with following error. I checked user, file permission and run the application from command line.
systemctl status sserpapi
× sserpapi.service - A small and simple erp backend (sserpapi)
Loaded: loaded (/etc/systemd/system/sserpapi.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2024-07-08 15:07:32 +06; 3s ago
Process: 6789 ExecStart=/home/saiful/sserpapi/.venv/bin/uvicorn --host 0.0.0.0 --port 8001 sserpapi.main:app (code>
Main PID: 6789 (code=exited, status=217/USER)
CPU: 2ms
Jul 08 15:07:31 office systemd[1]: Started A small and simple erp backend (sserpapi).
Jul 08 15:07:32 office systemd[6789]: sserpapi.service: Failed to determine user credentials: No such process
Jul 08 15:07:32 office systemd[6789]: sserpapi.service: Failed at step USER spawning /home/saiful/sserpapi/.venv/bin/u>
Jul 08 15:07:32 office systemd[1]: sserpapi.service: Main process exited, code=exited, status=217/USER
Jul 08 15:07:32 office systemd[1]: sserpapi.service: Failed with result 'exit-code'.
Systemd unit service file
[Unit]
Description=A small and simple erp backend (sserpapi)
After=network.target
[Service]
User=saiful # Replace with the user to run the service
WorkingDirectory=/home/saiful/sserpapi/sserpapi
Environment="PATH=/home/saiful/sserpapi/.venv/bin"
ExecStart=/home/saiful/sserpapi/.venv/bin/uvicorn --host 0.0.0.0 --port 8001 "sserpapi.main:app"
[Install]
WantedBy=multi-user.target
User exists
id saiful
uid=1000(saiful) gid=1000(saiful) groups=1000(saiful),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),108(lxd),998(docker)
File permission seems ok
ls -al /home/saiful/sserpapi/.venv/bin
total 100
drwxrwxr-x 2 saiful saiful 4096 Jul 7 10:59 .
drwxrwxr-x 5 saiful saiful 4096 Jul 7 10:52 ..
-rw-r--r-- 1 saiful saiful 9033 Jul 7 10:52 Activate.ps1
-rw-r--r-- 1 saiful saiful 2002 Jul 7 10:52 activate
-rw-r--r-- 1 saiful saiful 928 Jul 7 10:52 activate.csh
-rw-r--r-- 1 saiful saiful 2208 Jul 7 10:52 activate.fish
-rwxrwxr-x 1 saiful saiful 236 Jul 7 10:59 alembic
-rwxrwxr-x 1 saiful saiful 235 Jul 7 10:58 dotenv
-rwxrwxr-x 1 saiful saiful 246 Jul 7 10:59 email_validator
-rwxrwxr-x 1 saiful saiful 236 Jul 7 10:59 mako-render
-rwxrwxr-x 1 saiful saiful 256 Jul 7 10:59 normalizer
-rwxrwxr-x 1 saiful saiful 244 Jul 7 10:52 pip
-rwxrwxr-x 1 saiful saiful 244 Jul 7 10:52 pip3
-rwxrwxr-x 1 saiful saiful 244 Jul 7 10:52 pip3.10
-rwxrwxr-x 1 saiful saiful 244 Jul 7 10:59 py.test
-rwxrwxr-x 1 saiful saiful 235 Jul 7 10:59 pyrsa-decrypt
-rwxrwxr-x 1 saiful saiful 235 Jul 7 10:59 pyrsa-encrypt
-rwxrwxr-x 1 saiful saiful 233 Jul 7 10:59 pyrsa-keygen
-rwxrwxr-x 1 saiful saiful 256 Jul 7 10:59 pyrsa-priv2pub
-rwxrwxr-x 1 saiful saiful 229 Jul 7 10:59 pyrsa-sign
-rwxrwxr-x 1 saiful saiful 233 Jul 7 10:59 pyrsa-verify
-rwxrwxr-x 1 saiful saiful 244 Jul 7 10:59 pytest
lrwxrwxrwx 1 saiful saiful 7 Jul 7 10:52 python -> python3
lrwxrwxrwx 1 saiful saiful 16 Jul 7 10:52 python3 -> /usr/bin/python3
lrwxrwxrwx 1 saiful saiful 7 Jul 7 10:52 python3.10 -> python3
-rwxrwxr-x 1 saiful saiful 234 Jul 7 10:59 uvicorn
Can run the application manually from command line, same line that is in the ExecStart section of systemd service unit file
`/home/saiful/sserpapi/.venv/bin/uvicorn --host 0.0.0.0 --port 8001 "sserpapi.main:app"`
INFO: Started server process [6616]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8001 (Press CTRL+C to quit)