树莓派开机自动执行方法总结
1.rc.local
sudo vim /etc/rc.local
添加如下命令,fromrc.local.txt是我提前在/home/4by/目录下创建好的文件
echo "from rc.local" > /home/4by/fromrc.local.txt
重启后查看rc.local
4by@BY:~ $ cat fromrc.local.txt
from rc.local
4by@BY:~ $
2.使用systemd
创建一个新的 systemd 服务单元文件,通常存放在 /etc/systemd/system
目录下,比如 demo.service。内容如下
root@BY:~# cat /etc/systemd/system/demo1.service
[Unit]
Description=Custom setup
After=multi-user.target
[Service]
Type=oneshot
ExecStart=/bin/bash -c "echo "from service" > /home/4by/fromservice.txt"
[Install]
WantedBy=multi-user.target
root@BY:~#
然后你需要加载这个service服务文件,过程如下:
root@BY:~# sudo systemctl daemon-reload
root@BY:~# sudo systemctl enable demo1.service
Created symlink /etc/systemd/system/multi-user.target.wants/demo1.service → /etc/systemd/system/demo1.service.
root@BY:~#
重启查看/home/4by/fromservice.txt:
root@BY:/home/4by# cat fromservice.txt
from service
root@BY:/home/4by#
3.使用crontab
root@BY:/home/4by# crontab -e
添加@reboot+需要开机执行的脚本位置,以xxx.sh为例,如下
@reboot /home/4by/xxx.sh
以上三种就是比较常用的开机自动执行的方法,新版本系统好像不支持rc.local了,如果你的/etc目录下面没有rc.local的话,新系统提倡使用service服务。