方法一 rc.local文件
1.先创建一个简单的脚本,输出当前文件的时间
touch outNowTime.sh
#!/bin/bash
date > /root/nowTime.txt
2.编辑文件
vi /etc/rc.d/rc.local
# !/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
/root/outNowTime.sh &
/root/outNowTime.sh &
这里一定要加 &
3.给文件权限
chmod +x /etc/rc.d/rc.local
chmod +x /root/outNowTime.sh
一些错误
在开放web端口的时候可能会访问不通,那么可以选择开放端口或者永久关闭防火墙
查看防火墙状态
systemctl status firewalld.service
关闭防火墙
systemctl stop firewalld.service
永久关闭防火墙
systemctl disable firewalld.service
如果遇到了centos重启后卡在进度条的情况下,可以进入单用户模式,暂时把rc.local后台自启动的命令注释或者删除掉
开机时,大概启动到这里按"E"键,也可以提前就按着。
然后再这个位置加入 init=/bin/bash
然后按 ctrl+x 进入单用户模式
然后编辑rc.local的时候可能会遇到权限不够,输入如下命令即可编辑
mount -o remount,rw /
重新编辑你的rc.local文件后重启后就好了