centos的开启自启动问题,在服务可分为centos6和centos7两种情况。
- centos6 只需要赋予/etc/rc.d/rc.local文件执行权限,
chmod +x /etc/rc.d/rc.local 或chmod 777 /etc/rc.d/rc.local
这时centos将在启动时将执行 /etc/rc.d/rc.local中的命令
- centos7 需要赋予/etc/rc.d/rc.local文件执行权限,并且会有开机启动服务:rc-local
需要先服务权限: chmod +x /etc/rc.d/rc.local 或chmod 777 /etc/rc.d/rc.local
然后启动rc-local 服务, systemctl start rc-local
并设置为开机启动systemctl enable rc-local
若为赋予/etc/rc.d/rc.local文件执行权限,就启动rc-local 服务。会出现一下报错
可在/etc/rc.d/rc.local填写一个命令测试,开机是否执行该文件,例如:
touch /opt/local/test
重启后查看是否存在/opt/local/test来判断是否开机执行了/etc/rc.d/rc.local文件。
-
若/etc/rc.d/rc.local文件开机执行,但是还是有部分命令未执行,最大的问题可能是环境变量问题,
例如安装的jdk环境变量,在系统上可手动运行,但开机自动运行文件无法执行,这时可以写成shell脚本,在/etc/rc.d/rc.local文件中执行shell脚本,
在/etc/rc.d/rc.local中,写入 sh -x /opt/start.sh
在/opt/start.sh中配置为:
#!/bin/sh
export JAVA_HOME=/usr/local/jdk
export JRE_HOME= J A V