新建文件:
sudo vi /etc/systemd/system/myserver.service
写入:
[Unit]
Description=My Server
After=network.target
[Service]
ExecStart=/usr/bin/python3 /path/to/server.py #文件路由
WorkingDirectory=/path/to/ #文件目录
Restart=always
[Install]
WantedBy=multi-user.target
接下来,重新加载systemd配置以使其识别和启用新的service单元文件:
sudo systemctl daemon-reload
启用服务,使其在系统启动时自动运行:
sudo systemctl enable myserver
启动服务:
sudo systemctl start myserver
确认服务已经成功启动:
sudo systemctl status myserver
需要关闭防火墙:
sudo systemctl stop firewalld #临时关闭防火墙
sudo systemctl disable firewalld #禁止防火墙