配置上电自启动文件
-
创建一个服务单元文件
sudo nano /etc/systemd/system/myservice.service
添加以下内容:
[Unit]
Description=My Custom Service
After=network.target
[Service]
ExecStart=/usr/bin/python3 /home/ubuntu/usart.py
WorkingDirectory=/home/ubuntu
Restart=always
User=ubuntu
Group=ubuntu
[Install]
WantedBy=multi-user.target
主要修改:ExecStart、WorkingDirectory、User、Group
查看当前目录{如果不确定自己的代码在哪个目录可以执行这个命令查看当前目录)
pwd
重新加载 systemd 配置:
sudo systemctl daemon-reload
启用服务并启动: 启用服务后,系统启动时会自动执行脚本。
sudo systemctl enable myservice.service
sudo systemctl start myservice.service
查看脚本的状态:
sudo systemctl status myservice.service
重启系统并验证脚本是否自启动:
sudo reboot
-
禁用服务
你可以使用 systemctl 命令禁用服务,这样服务在下次开机时就不会自动启动了,但服务本身不会被删除。
sudo systemctl disable myservice.service
这会删除 /etc/systemd/system/multi-user.target.wants/myservice.service 的符号链接,从而防止服务在下次启动时自动启动。
如果你只想停止当前正在运行的服务,但保留开机自启动配置,可以运行:
sudo systemctl stop myservice.service
-
恢复自启动
如果你之后需要恢复自启动,只需再次启用服务即可:
sudo systemctl enable myservice.service
然后,你可以选择重新启动服务:
sudo systemctl start myservice.service
2142

被折叠的 条评论
为什么被折叠?



