3 ways to configure services to start automatically at boot on CentOS7.6

I. Introduction

Various middleware installed on the server generally need to be configured to start automatically at boot in order to prevent the consequences of unexpected downtime and restart. However, the installation process of some middleware does not provide instructions for configuring auto-start at boot. Today I will talk to you about
several ways to configure services to start automatically on Centos.

Centos下配置服务开机自启动有3种方式:
方式一:直接在/etc/rc.d/rc.local中添加服务启动命令;
方式二:通过chkconfig配置服务自启动;
方式三:Centos7通过systemctl enble配置服务自启动。

2. Operation process

2.1 Add the service startup command in /etc/rc.d/rc.local

The /etc/rc.d/rc.local script will be automatically executed when the Centos system starts, so you can place commands that need to be executed after booting directly here.

#vi /etc/rc.d/rc.local

Insert image description here
If you want to make it simpler, you can directly add the service startup command to /etc/rc.d/rc.local:

Insert image description here

You can also write your own script to start the service. Since the system restarts as the root user, it is necessary to ensure that the root user has script execution permissions.

1) Write a script to start the service

#vi /opt/script/zabbixstart.sh

Enter the startup script content:

#!/bin/bash
/home/ampdcp/snc_product/zabbix_proxy/sbin/zabbix_proxy -c
/home/ampdcp/snc_product/zabbix_proxy/etc/zabbix_proxy.conf

2) Give the script executable permissions

#chmod +x /opt/script/zabbixstart.sh

3) Open the /etc/rc.d/rc.local file and add the following content at the end

#/opt/script/autostart.sh

4) In centos7, the permissions of /etc/rc.d/rc.local are reduced, so you need to execute the following command to give it executable permissions

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

2.2 Configuration through chkconfig

Before CentOS7, you can configure the auto-start service at boot through chkconfig.

chkconfig related commands:

chkconfig –-add xxx //把服务添加到chkconfig列表;
chkconfig --del xxx //把服务从chkconfig列表中删除;
chkconfig xxx on //开启开机自动启动;
chkconfig xxx off //关闭开机自动启动;
chkconfig --list //查看所有chklist中服务;
chkconfig --list xxx 查看指定服务。

The concepts of chkconfig run level and startup sequence:
Insert image description here
0 to 6 here actually refer to the level of the service. Specify the execution level in which the system service should be turned on or off.

等级0表示:表示关机;
等级1表示:单用户模式;
等级2表示:无网络连接的多用户命令行模式;
等级3表示:有网络连接的多用户命令行模式;
等级4表示:不可用;
等级5表示:带图形界面的多用户模式;
等级6表示:重新启动。

For example, the following command:
//Set mysqld to run services on startup at levels 3 and 5:

chkconfig --level 35 mysqld on

//Set the network service to start automatically at boot, which will set all levels 2 to 5 to on:

chkconfig network on

Insert image description here
The service startup sequence configured by chkconfig will eventually be reflected in the /etc/rc.d/ directory.

Use chkconfig to configure kibana to start the instance automatically at boot:

1) In the /etc/init.d directory, create a new script kibana. The script content is as follows

#!/bin/bash

# chkconfig: 2345 98 02
# description: kibana

Note: Each service managed by chkconfig needs to add two or more lines of comments to the corresponding script under init.d. The first line tells chkconfig the default startup runlevel and the start and stop priorities. If a service does not start at any runlevel by default, use - instead of the runlevel. The second line describes the service and can be commented with \ across lines.

Explanation: Configure the kibana service to execute the script in order 98 at levels 2, 3, 4, and 5.

KIBANA_HOME=/usr/local/kibana-6.2.4-linux-x86_64
case $1 in
 start)
         $KIBANA_HOME/bin/kibana &
         echo "kibana start"
         ;;
 stop)
    kibana_pid_str=`netstat -tlnp |grep 5601 | awk '{print $7}'`
    kibana_pid=`echo ${
     
     kibana_pid_str%%/*}`
    kill -9 $kibana_pid
    echo "kibana stopped"
    ;;
 restart)
    kibana_pid_str=`netstat -tlnp |grep 5601 | awk '{print $7}'`
    kibana_pid=${kibana_pid_str%%/*}
    kibana_pid=`echo ${
     
     kibana_pid_str%%/*}`
    kill -9 $kibana_pid
    echo "kibana stopped"

    $KIBANA_HOME/bin/kibana &
    echo "kibana start"
    ;;
 status)
    kibana_pid_str=`netstat -tlnp |grep 5601 | awk '{print $7}'`
    if test -z $kibana_pid_str; then
       echo "kibana is stopped"
    else
       pid=`echo ${
     
     kibana_pid_str%%/*}`
       echo "kibana is started,pid:"${pid}
    fi
    ;;
*)
    echo "start|stop|restart|status"
    ;;
esac

2) Increase the executable permissions of the script

#chmod +x kibana

3) View chkconfig list

#chkconfig --list

4) Add the service to the chkconfig list

#chkconfig --add kibana

5) Set kibana service to start automatically

#chkconfig kibana on //开启开机自动启动。

#View kibana service self-start status:

#chkconfig --list kibana //如果2~5都是on,就表明会自动启动了。

2.3 Centos7 configures kibana service to start automatically through systemctl

After Centos7, it is more recommended to control services through systemctl.

Insert image description here

2.3.1 Directory introduction of systemctl service

We know that service management is through systemd, and most of systemd's configuration files are placed in the /usr/lib/systemd/ directory. However, Red Hat official documents point out that the files in this directory are mainly settings provided by the original software, and it is recommended not to modify them! The location to be modified should be placed in the /etc/systemd/system/ directory.

2.3.2 Create kibana startup service software

#cd /etc/systemd/system/
#vim kibana.service

Script contents:

[Unit]
Description=kibana
After=network.target
[Service]
Type=forking
User=root
Group=root
ExecStart=/etc/init.d/kibana start
ExecReload=/etc/init.d/kibana restart
ExecStop=/etc/init.d/kibana stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target

The commands of ExecStart, ExecReload, and ExecStop are implemented by configuring the kibana script in the /etc/init.d directory above.

  • [Service]'s start, restart, and stop commands all require absolute paths.
  • [Install] Settings related to service installation can be set to multiple users.

Parameter Description:

  • Description: describes the service;
  • After: describes the service category;
  • [Service] Settings of service operating parameters;
  • Type=forking is the form of background operation;
  • The User service starts the user;
  • The Group service starts the user group;
  • ExecStart is the specific running command of the service;
  • ExecReload is the restart command;
  • ExecStop is the stop command;
    PrivateTmp=True means allocating independent temporary space to the service.

2.3.3 Grant execution permissions

#chmod 754 kibana.service

2.3.4 Starting, stopping and starting services

//Reload the configuration file of a certain service. If a service is newly installed, it is managed by systemctl. If the service program configuration file of the new service takes effect, it needs to be reloaded.

#systemctl daemon-reload
//查看服务状态
#systemctl status kibana.service

Service status description:

Insert image description here

Guess you like

Origin blog.csdn.net/lcy1619260/article/details/132665303