编写开机自动执行的脚本auto.sh
#!/bin/sh#chkconfig: 2345 20 80#description:Server reboot.Execute auto.sh#client post port 80 redirect 9527#sudo iptables -A PREROUTING -t nat -i eth0 -p tcp —dport 80 -j REDIRECT —to-port 9527#auto.sh logDIR=/home/linux-niceecho "execute auto.sh" > $DIR/auto_log.txt
chkconfig命令
chkconfig命令检查、设置系统的各种服务。这是Red Hat公司遵循GPL规则所开发的程序,它可查询操作系统在每一个执行等级中会执行哪些系统服务,其中包括各类常驻服务
等级0表示:表示关机等级1表示:单用户模式等级2表示:无网络连接的多用户命令行模式等级3表示:有网络连接的多用户命令行模式等级4表示:不可用等级5表示:带图形界面的多用户模式等级6表示:重新启动
#!/bin/sh 告诉linux服务器这是一个.sh脚本**2345**对应linux服务器的2345阶段能执行auto.sh脚本20表示启动优先级序号 80表示关闭优先级序号(就这个不太懂 有哪位大佬能提供资料吗)
[root@VM_158_86_centos ~]# cd /etc/rc.d/init.d/ 进入该文件夹[root@VM_158_86_centos init.d]# vi auto.sh 编写脚本[root@VM_158_86_centos init.d]# vi auto.sh [root@VM_158_86_centos init.d]# chmod +x auto.sh 给脚本添加执行权限[root@VM_158_86_centos init.d]# chkconfig --add auto.sh 添加脚本至开机自启[root@VM_158_86_centos init.d]# chkconfig auto.sh on 开启[root@VM_158_86_centos init.d]# sudo reboot 重启
验证
请事先创建好/home/linux-nice/auto_log.txt 文件
然后重启服务器 sudo reboot
auto_log.txt是否输出了”execute auto.sh”
结果打印出来了 说明开机自动执行了该脚本
prefect
补充:
有可能linux系统没有/etc/rc.d/init.d/ 文件夹 就在/etc/rc.local
sh /path/xxx.sh命令
如果觉得《linux开机自动执行.sh脚本》对你有帮助,请点赞、收藏,并留下你的观点哦!