一、写一个testapi.service文件,上传到/etc/systemd/system目录下
[Unit]
Description=My Python Web API
After=network.target
[Service]
User=liu
WorkingDirectory= /home/myuser/myapp/myproject
ExecStart=/home/myuser/myapp/venv/bin/python /home/myuser/myapp/test.py
Restart=always
[Install]
WantedBy=multi-user.target
二、你可以通过以下命令检查该服务是否存在:
systemctl list-unit-files | grep testapi
运行后提示
systemctl list-unit-files | grep testapi
testapi.service disabled enabled
从你提供的输出来看,testapi服务存在,但是它处于禁用状态。你需要启用该服务,然后才能够使用它。
你可以使用以下命令启用该服务:
sudo systemctl enable testapi
然后,你可以使用以下命令启动该服务:
sudo systemctl start testapi
如果你还想在系统启动时自动启动该服务,你可以将其添加到启动项中:
sudo systemctl enable --now testapi
这将启用并立即启动该服务,并在系统启动时自动启动该服务。
sudo systemctl stop testapi