unix 无法将python脚本作为服务systemctl Cent运行

6ojccjat  于 2022-11-04  发布在  Unix
关注(0)|答案(1)|浏览(223)

我有以下systemctl服务

[Unit]
Description=Ml api

[Service]

# user=root

ExecStart=/usr/local/bin/python3.9 -u /home/a.nikitin@corp.bsv.legal/bsv_ml_api/app.py

ExecStop=/bin/kill -15 $MAINPID
Type=forking

# KillMode=process

# SyslogIdentifier=ml-api

# SyslogFacility=daemon

Restart=on-failure

[Install]
WantedBy=multiuser.target

当我运行它时,我得到了一个错误。sudo journalctl -u ml.service -e显示

ml.service - Ml api
   Loaded: loaded (/usr/lib/systemd/system/ml.service; disabled; vendor preset: disabled)
   Active: failed (Result: start-limit) since Fri 2022-06-10 11:38:38 MSK; 1 day 23h ago
 Main PID: 77614 (code=exited, status=203/EXEC)

Jun 10 11:38:38 srv-ml-api.corp.bsv.legal systemd[1]: Unit ml.service entered failed state.
Jun 10 11:38:38 srv-ml-api.corp.bsv.legal systemd[1]: ml.service failed.
Jun 10 11:38:38 srv-ml-api.corp.bsv.legal systemd[1]: ml.service holdoff time over, scheduling restart.
Jun 10 11:38:38 srv-ml-api.corp.bsv.legal systemd[1]: Stopped Ml api.
Jun 10 11:38:38 srv-ml-api.corp.bsv.legal systemd[1]: start request repeated too quickly for ml.service
Jun 10 11:38:38 srv-ml-api.corp.bsv.legal systemd[1]: Failed to start Ml api.
Jun 10 11:38:38 srv-ml-api.corp.bsv.legal systemd[1]: Unit ml.service entered failed state.
Jun 10 11:38:38 srv-ml-api.corp.bsv.legal systemd[1]: ml.service failed.

但是,如果我直接运行

/usr/local/bin/python3.9 -u /home/a.nikitin@corp.bsv.legal/bsv_ml_api/app.py

一切正常,脚本启动

INFO:     Started server process [4401]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:1111 (Press CTRL+C to quit)

我不知道问题出在哪里,是FastAPI + uvicorn应用。

omjgkv6w

omjgkv6w1#

我需要指定python env。

Environment="PYTHONPATH=$PYTHONPATH:/home/a.nikitin@corp.bsv.legal/.local/lib/python3.9/site-packages"

相关问题