Centos 7關(guān)于rc.local腳本命令開機不執(zhí)行及指定用戶啟動的解決方法

1.開機不啟動

在實際生產(chǎn)場景中,我們喜歡在安裝了一些軟件服務(wù)后,將軟件設(shè)置為開機自啟動,設(shè)置為開機自啟動有兩種方法:

1)? 設(shè)置為chkconfig,可以編寫腳本,查看設(shè)置開機自啟動的命令 –add表示添加程序自啟動, --list表示查看。

以后的程序如果需要使用chkconfig開機自啟動,那么需要在啟動程序中加入三行:

# chkconfig:2345 20 80

#description: Saves and restores system entropy pool for \

#? ? ? ? ? ? ? higher quality random numbergeneration.

? ? ? ? ? ? ? ? 第一行中的20,80是啟動級別,不能與其他程序一樣,因此,需自定義設(shè)置。

2)? 配置在/etc/rc.local文件中。直接將軟件服務(wù)的啟動命令寫在rc.local文件

注意:編輯完rc.local文件后,一定要給rc.local文件執(zhí)行權(quán)限,否則開機時不會執(zhí)行rc.local文件中腳本命令

chmod+x /etc/rc.d/rc.local

注意此處,是/etc/rc.d/rc.local,而不是/etc/rc.local,如果給/etc/rc.local執(zhí)行權(quán)限是無效的,因為/etc/rc.local是軟鏈接,真正的文件是/etc/rc.d/rc.local

2.關(guān)于在rc.local文件中指定用戶執(zhí)行腳本命令

使用su命令即可,命令格式:

su - username -c “your-cammand” ,如:

[jiakeke@mongodb ~]$ cat /etc/rc.local

#!/bin/bash

# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES

#

# It is highly advisable to create own systemdservices or udev rules

# to run scripts during boot instead of using thisfile.

#

# In constrast to previous versions due to parallelexecution during boot

# this script will NOT be run after all otherservices.

#

# 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

/bin/systemctl start iptables.service

#startup mongodb

/bin/su - xiaoyao -c? "/mnt/mongodb/bin/mongod --config/mnt/mongodb/bin/mongodb.conf"

注意:指定用戶執(zhí)行的腳本(程序)目錄,該用戶必須有管理該腳本(程序)目錄(文件)的權(quán)限。

最好將該腳本(程序)目錄的所有權(quán)給該用戶:

chown -R xiaoyao.xiaoyao /mnt/mongodb

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容