首先编写好自己的python脚本
然后执行命令vi xxxx.service
输入以下内容
[Unit]
Description = xxxx #描述#
After = network.target
[Service]
ExecStart = /usr/bin/python -u python脚本名
WorkingDirectory = /home #脚本所在目录#
StandardOutput = inherit
StandardError = inherit
Restart = always
User = pi #系统登录用户名#
[Install]
WantedBy=multi-user.target
然后把 xxx.service 复制到 /etc/systemd/system/
sudo cp xxx.service /etc/systemd/system/xxx.service
最后
启动该服务 sudo systemctl start xxx.service
停止该服务 sudo systemctl stop xxx.service
查看运行状态 sudo systemctl status xxx.service
设置开机运行 sudo systemctl enable xxx.service