目录
使用 systemd
创建 Python 脚本
chmod +x /path/to/your_script.py
创建一个 systemd
服务文件
vim /etc/systemd/system/my-python-script.service
[Unit]
Description=My Python Startup Script
[Service]
ExecStart=/usr/bin/python3 /path/to/your_script.py
WorkingDirectory=/path/to
Restart=on-failure
User=your-username
[Install]
WantedBy=multi-user.target
ExecStart
是 Python 解释器路径和你的脚本路径。WorkingDirectory
是你的脚本所在目录。User
是运行脚本的用户。
重新加载 systemd
配置
sudo systemctl daemon-reload
启用并启动服务
sudo systemctl enable my-python-script.service
sudo systemctl start my-python-script.service