Configure the service to start automatically in three ways

Method a: ln -s flexible connection to establish

  a total of seven kinds of operating level in CentOS can be set in / etc / inittab file, 7 run level corresponding /etc/rc.d/rc[0-6]. d seven directory

   ps: You may also find in the / etc has the same 7 folder is /etc/rc[0-6].d, by viewing is actually found /etc/rc[0-6].d / etc / rc.d / rc [0-6] .d flexible connection, but in order to maintain compatibility and Unix

  This directory corresponds to 7 seven different run levels, that is to say in the content of each directory indicates you should start at that level run or shut down services. For example, we look at the contents of the directory /etc/rc.d/rc5.d

   Of course, you may also find that these files are named in the following format [KS] [1-100] servicename, which indicates when the system run level is 5, it will turn off all services that begin with K, and start all S at the beginning of the service, in fact, these services are connected to a flexible service in /etc/init.d/, so the real start of the service is one of the /etc/init.d directory service, and to the K / S is beginning to establish flexible connections in order to show that the behavior of the corresponding level should be taken, followed by the K / S behind the numbers indicate the oN / oFF priority services.

    So, if we wrote a script code as cleanupd, need to level 3 boot time from the start, then we just need an executable script file into the cleanupd /etc/init.d directory, and the corresponding the run-level directories in the establishment of an /etc/rc.d/rc3.d/ plus the number of S is connected to the beginning of the soft /etc/init.d/cleanupd to as

[root @ localhost ~] # ln - s /etc/init.d/cleanupd /etc/rc.d/rc3.d/S95cleanup

obviously, this arrangement more complicated, we want only suitable for DIY script service.
Method 2: chkconfig

view of the above manual operation is relatively cumbersome, specifically provided in the CentOS chkconfig command to set or cancel the service boot from the start. For example, a query for all start-up services:


Use chkconfig chkconfig -list or service can be seen that each system is already set in the opened and closed states of the individual operating levels. If we want to set up a service on or off from the start, then you can just use the following format

chkconfig servicename on / off

   , such as:
1. Review the sshd

2. 将 sshd 设置为开机自启动:

[root@host ~]# chkconfig sshd on


取消 sshd 的开机自启动,只需要将 on 改为 off 即可:

[root@host ~]# chkconfig sshd off
3.值得注意的是,如果这个服务尚未被添加到 chkconfig 列表中,则现需要使用 –-add 参数将其添加进去:

[root@host ~]# chkconfig --add sshd

4.如果要查询当前所有自动启动的服务,可以输入:

[root@host ~]# chkconfig --list

5.如果只想看指定的服务,只需要在 “–-list” 之后加上服务名就好了,比如查看httpd服务是否为自动启动:

[root@host ~]# chkconfig --list httpd

[root@host ~]# chkconfig --list httpd
httpd           0:off   1:off   2:off   3:off   4:off   5:off   6:off

此时0~6均为off,则说明httpd服务不会在系统启动的时候自动启动。我们输入:

[root@host ~]# chkconfig httpd on
则此时为:

[root@host ~]# chkconfig --list httpd
httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off

这个时候2~5都是on,就表明会自动启动了。
方法三:修改 /etc/rc.d/rc.local 这个文件:

例如将 apache、MySQL、samba、svn 等这些服务的开机自启动问题一起搞定:

    vi/etc/rc.d/rc.local

添加以下命令

    /usr/sbin/apachectlstart
    /etc/rc.d/init.d/mysqldstart
    /etc/rc.d/init.d/smbstart
    /usr/local/subversion/bin/svnserve-d
————————————————
版权声明:本文为CSDN博主「Peter_Lv1」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_40606798/article/details/82286273

 

Guess you like

Origin www.cnblogs.com/tanxiaojun/p/12081509.html