[Linux系统] 将python程序配置为服务

编写xxx.service

cd /etc/systemd/system/
sudo nano xxx.service

填写以下内容:

[Unit]
Description=XXXX
After=network.target

[Service]
ExecStart=/bin/bash /opt/leo/py_space/KeyPersonRecServer/run.sh
WorkingDirectory=/opt/leo/py_space/KeyPersonRecServer
Restart=always

[Install]
WantedBy=multi-user.target

reload配置:

sudo systemctl daemon-reload

服务设置

sudo systemctl enable xxxx.service
sudo systemctl start xxxx.service
sudo systemctl restart xxxx.service
sudo systemctl stop xxxx.service

服务使用指定用户运行

[Service]
User=leo
Group=leo

运行服务前添加环境变量

[Service]
EnvironmentFile=/opt/leo/py_space/SysGeneralServer/env_file

env_file:

LD_LIBRARY_PATH=/opt/leo/py_space/SysGeneralServer/text2image/lib:/home/leo/.conda/envs/leo_env2/lib
PATH=/usr/local/ffmpeg/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin

在环境变量文件中,使用键值对来设置服务使用的环境变量,主要是为了解决非激活环境下运行python程序,无法获取完整环境变量所导致的报错。

 

 

 

==

posted @ 2024-04-11 14:41  风间悠香  阅读(72)  评论(0编辑  收藏  举报