Centos 7关于rc.local脚本命令开机不执行及指定用户启动的解决方法

1.开机不启动

在实际生产场景中,我们喜欢在安装了一些软件服务后,将软件设置为开机自启动,设置为开机自启动有两种方法:

1)  设置为chkconfig,可以编写脚本,查看设置开机自启动的命令 –add表示添加程序自启动, --list表示查看。

以后的程序如果需要使用chkconfig开机自启动,那么需要在启动程序中加入三行:

# chkconfig:2345 20 80

#description: Saves and restores system entropy pool for \

#              higher quality random numbergeneration.

                第一行中的20,80是启动级别,不能与其他程序一样,因此,需自定义设置。

2)  配置在/etc/rc.local文件中。直接将软件服务的启动命令写在rc.local文件

注意:编辑完rc.local文件后,一定要给rc.local文件执行权限,否则开机时不会执行rc.local文件中脚本命令

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

注意此处,是/etc/rc.d/rc.local,而不是/etc/rc.local,如果给/etc/rc.local执行权限是无效的,因为/etc/rc.local是软链接,真正的文件是/etc/rc.d/rc.local

2.关于在rc.local文件中指定用户执行脚本命令

使用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"

注意:指定用户执行的脚本(程序)目录,该用户必须有管理该脚本(程序)目录(文件)的权限。

最好将该脚本(程序)目录的所有权给该用户:

chown -R xiaoyao.xiaoyao /mnt/mongodb

你可能感兴趣的:(Centos 7关于rc.local脚本命令开机不执行及指定用户启动的解决方法)