【问题标题】:How can i run a python file as service python3我如何将 python 文件作为服务 python3 运行
【发布时间】:2018-01-30 04:09:12
【问题描述】:
我有一个 python 脚本,我必须作为服务/后台进程运行,我尝试了 python-daemon 但它给了我错误是否有任何好的方法可以将它作为服务运行以便脚本可以运行在后台接受消息和处理文件?
【问题讨论】:
标签:
python-3.x
python-daemon
【解决方案1】:
我在 /lib/systemed/system 中创建了一个 sytemctl 服务文件
文件内容是
[Unit]
Description=RPC SERVER
After=network.target
[Service]
User=ubuntu
WorkingDirectory=/home/ubuntu/
Type=simple
ExecStart=/usr/bin/ python3 /home/ubuntu/rpc.py
[Install]
WantedBy=multi-user.target
/home/ubuntu/rpc.py 是我的python脚本路径
我将文件保存为 rpcservertest.service
所以我可以使用
sudo systemctl start rpcservertest.service
停止
sudo systemctl stop rpcservertest.service
并获得状态
sudo systemctl status rpcservertest.service