这个视频讲得很到位
下面是视频中的代码,方便大家复制
sudo nano /etc/systemd/system/rcLocal.service
然后键入以下内容:
[Unit]
Description=/etc/rc.local Compatibility
After=createap.service #服务启动的顺序在createap.service服务的后面(可填写其他你需要的,也可以不写这一行)
ConditionPathExists=/etc/rc.local
[Service]
Type=forking
ExecStart=/etc/rc.local #这是开机自启程序的位置,我是在这个文件里面添加了我想启动的程序
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
[Install]
WantedBy=multi-user.target
复制以上代码后,执行以下命令:
sudo systemctl enable rclocal.service
sudo chmod +x /etc/rc.local
下面可以在/etc/rc.local文件中加入自启的程序了:
sudo nano /etc/rc.local
在exit 0前面写入你的程序,例如:
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
/home/pi/ship/RaspberrryPi-Project-for-PiCar/MainProgram.py&
/home/pi/ship/RaspberrryPi-Project-for-PiCar/FramesSendProgram.py&
exit 0
·····························································································································································································································································································································
经过测试后发现在rc.local中添加的程序会开机自启,即便你使用sudo systemctl disable rcLocal关掉rcLocal服务,里面的程序还是会自启。