首先編寫好自己的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