Centos7 rc.loca添加自启动不生效的问题

Centos7 rc.loca添加自启动不生效的问题

背景:
我自己写了一个mongodb启动的shell脚本startMongoDB.sh
cat /opt/mongodb/bin/startMongoDB.sh #内容如下

/#!/bin/bash

mongod --config /opt/mongodb/mongodb.conf

然后在/etc/rc.d/rc.local中添加了启动项(如果是Unbuntu系统,则自己在/etc/init.d/目录下创建自启动脚本)

重启后发现无法启动,脚本内容如下:

# cat /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

/opt/mongodb/bin/startMongoDB.sh

尝试逐步排查下列可能导致启动失败的情况

rc.loca权限是否满足
# ls -l /etc/rc.d/rc.local 
-rw-r--r--. 1 root root 523 Jun 11 00:45 /etc/rc.d/rc.local

如果没有执行权限,需要增加执行权限
# chmod u+x rc.local 
-rwxr--r--. 1 root root 523 Jun 11 00:45 rc.local

reboot发现重启仍然没有生效

查看系统日志看能否找到错误信息
cat /var/log/message | grep rc.local
找到相关错误信息
Jun 11 02:56:36 localhost systemd: Starting /etc/rc.d/rc.local Compatibility...
Jun 11 02:56:36 localhost rc.local: /opt/mongodb/bin/startMongo.sh: line 3: mongod: command not found
Jun 11 02:56:36 localhost systemd: rc-local.service: control process exited, code=exited status=127

原因找到了,日志提示 mongod 命令无法识别。
修改startMongo.sh脚本,启动命令mongod改为使用绝对路径“/opt/mongodb/bin/mongod”
vi /opt/mongodb/bin/startMongoDB.sh

/#!/bin/bash

/opt/mongodb/bin/mongod --config /opt/mongodb/mongodb.conf

reboot重启,问题解决。
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章