简书链接:linux中让一个sh脚本开机运行通过编写service实现。
文章字数:190,阅读全文大约需要1分钟
模仿开源源码写了一个mjpgstream-service.service发现可以用,第一次因为资源busy不得不重启释放资源了。
主要是想让我的设备开机启动就跑摄像头流,这样就可以进行相机查看。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| [Unit] Description=mjpg-streamer Daemon Documentation=https://github.com/jacksonliam/mjpg-streamer After=network.target network-online.target Wants=network-online.target Requires=network.target
[Service] Type=notify User=daemon Group=daemon WorkingDirectory=/opt/mjpg-streamer/mjpg-streamer-experimental/ ExecStart=/opt/mjpg-streamer/mjpg-streamer-experimental/start.sh StandardOutput=null StandardError=journal Environment=PYTHONUNBUFFERED=1 Restart=always RestartSec=3
[Install] WantedBy=multi-user.target
|
然后创建快捷方式到systemd
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
sudo ln -s /opt/startup/mjpgstream-service.service /etc/systemd/system/mjpgstream-service.service
sudo systemctl daemon-reload
# tell system that it can start our script at system startup during boot sudo systemctl enable mjpgstream-service.service
# start the script running sudo systemctl start mjpgstream-service.service
# check to make sure all is ok with the start up sudo systemctl status mjpgstream-service.service
|
干掉服务
,其实等同于删掉 /etc/systemd/system/mjpgstream-service.service
输入
1
| sudo systemctl unmask mjpgstream-service.service
|
如果启动提示service is masked
可能因为里面的内容不正确,我第一次是因为是空的。
查看错误
1
| sudo journalctl -u robotcar.service -e
|
权限不够问题 i2c
1
| sudo usermod -aG i2c daemon
|
或者提升root组
