1.编写一个我们需要做事的脚本
#!/bin/bash
# 打印 "Hello"
echo "Hello,Mr.Phor"
# 为了更好的能看到效果 我们把这段文本放置到一个文件中 如果重启能够看到 /a.txt文件 我们实验成功
echo "hahahahahahahaha" > /a.txt
2.开机自启动文件设置
这个文件路径在:
/etc/rc.d/rc.local
我们在他的最后一行添加代码:
/start_service.sh
为什么这么做? 因为我们步骤一做了一个脚本在根目录,名字为:start_service.sh
我们在这个开机可以执行的脚本调用我们的脚本, 效果如下:
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
/start_service.sh
3.赋权:执行
注意了 我们自己做的脚本需要有执行权力 开机自启动的脚本也需要给予执行权力(默认它没有执行权力)
chmod +x /start_service.sh
chmod +x /etc/rc.d/rc.local
4.验证
接下来重启 检验
reboot
至此,我们实验成功!