【Python】如何在Ubuntu上设置Python脚本开机自启
    		       		warning:
    		            这篇文章距离上次修改已过440天,其中的内容可能已经有所变动。
    		        
        		                
                在Ubuntu上设置Python脚本开机自启动,可以通过创建一个系统服务来实现。以下是步骤和示例代码:
- 创建一个新的服务文件。
sudo nano /etc/systemd/system/your-service.service- 在这个文件中添加以下内容:
[Unit]
Description=Your Python Script
After=network.target
 
[Service]
ExecStart=/usr/bin/python3 /path/to/your/script.py
 
[Install]
WantedBy=multi-user.target将your-service.service替换为你的服务名,Description中填写描述,ExecStart中填写你的Python脚本的正确路径。
- 重新加载systemd管理器配置。
sudo systemctl daemon-reload- 启用你的服务,使其开机自启。
sudo systemctl enable your-service.service- (可选)立即启动服务而不需要重启。
sudo systemctl start your-service.service确保你的Python脚本具有执行权限:
chmod +x /path/to/your/script.py替换/path/to/your/script.py为你的Python脚本的实际路径。
评论已关闭