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重啓,問題解決。
 

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