Summary of Centos boot self-start configuration methods


foreword

All kinds of middleware installed on the server generally need to be configured to start automatically at boot. However, the installation process of some middleware does not provide related documentation for configuring boot-up self-starting.
Today, I will summarize the three ways to configure the service to start automatically after booting under Centos.


1. Several ways to configure boot self-start on Centos

  • Method 1: Add the service startup command directly in /etc/rc.d/rc.local
  • Method 2: Configure the service to start automatically through chkconfig
  • Method 3: Centos7 configures the service to start automatically through systemctl enble

2. Practical demonstration

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 put the commands that need to be executed after booting directly here.

Example: configure startup apollo

vi /etc/rc.d/rc.local

insert image description here
If you want to be simpler, you can directly add the startup command of the service to /etc/rc.d/rc.local as above.

You can also write your own scripts for service startup. Since the restart is performed by the root user, it is necessary to ensure that the root user has the script execution permission.

1), write the script to start the service
vi /opt/script/autostart.sh

#!/bin/bash
/root/Downloads/docker-quick-start/docker-compose up -d

2) Give the script executable permission (/opt/script/autostart.sh is your script path)

chmod +x /opt/script/autostart.sh

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

/opt/script/autostart.sh

3) 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. Configure through chkconfig

Before CentOS7, you can configure boot self-starting services 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 concept of chkconfig run level level and startup sequence:
chkconfig --listinsert image description here
0 to 6 here actually refers to the level of the service.

–level<level code> Specifies the execution level in which the system service should be turned on or off.
Level 0 means: shutdown
Level 1 means: single user mode
Level 2 means: multi-user command line mode without network connection
Level 3 means: multi-user command line mode with network connection
Level 4 means: unavailable
Level 5 means: with GUI multi-user mode
Level 6 means: reboot

For example, the following command:

//设定mysqld在等级3和5为开机运行服务
chkconfig --level 35 mysqld on 

//设置network服务开机自启动,会把2~5的等级都设置为on
chkconfig network on

Indicates that the startup configuration is successful.
insert image description here

What does the startup sequence of services refer to?
服务的启动顺序是指在服务器启动后服务启动脚本执行的顺序。
Instructions based on the system default service network:

cat /etc/init.d/network

insert image description here
It # chkconfig: 2345 10 90is used to specify the startup sequence of services at each level.
The meaning of this configuration is that the startup sequence of the network service at levels 2, 3, 4, and 5 is 10, and the startup sequence at levels 1 and 6 is 90.

The service startup sequence configured by chkconfig will finally /etc/rc.d/be reflected in the directory:

cd /etc/rc.d/

insert image description here
文件中脚本命名规则,首字母K表示关闭脚本,首字母S表示启用脚本,数字表示启动的顺序.

chkconfig configuration example
Generally, the official configuration of kibana does not introduce how to configure the boot-up auto-start. Here I configure kibana to start automatically after booting to illustrate.

1. In the /etc/init.d directory, create a new script kibana

cd /etc/init.d
vi kibana

The script content is as follows:

#!/bin/bash

# chkconfig: 2345 98 02
# description:  kibana

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

Note ⚠️:
Each service managed by chkconfig needs to add two or more lines of comments to the script under the corresponding init.d.
The first line tells chkconfig what runlevel to start by default and the priority to start and stop. 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 across lines with \.

#!/bin/bash
#chkconfig:2345 98 02
#description:kibana

Explanation:
When configuring the kibana service at levels 2, 3, 4, and 5, the script execution order is 98, and
at levels 1 and 6, the script execution order is 01.

2. Increase the executable permission of the script

chmod +x kibana

3. View chkconfig list

chkconfig --list 

4. Add the service to the chkconfig list

chkconfig --add kibana

insert image description here

5. Set the kibana service to start automatically

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

6. View the self-starting status of the kibana service

chkconfig --list kibana

如果2~5都是on,就表明会自动启动了
insert image description here
7. Service start, stop, restart and status view

//查看服务状态
service kibana status
//服务启动
service kibana start
//服务停止
service kibana stop
//服务重启
service kibana restart

insert image description here

3. Centos7 configures the service to start automatically through systemctl enble

After Centos7, it is recommended systemctlto control the service through.

Task old order new directive
make a service start automatically Chkconfig --level 3 httpd on systemctl enable httpd.service
Prevent a service from starting automatically chkconfig --level 3 httpd off systemctl disable httpd.service
Check service status service httpd status systemctl status httpd.service (service details)
systemctl is-active httpd.service (only shows whether Active)
show all started services chkconfig --list systemctl list-unit-files
systemctl list-units --type=service
start a service service httpd start systemctl start httpd.service
stop a service service httpd stop systemctl stop httpd.service
restart a service service httpd restart systemctl restart httpd.service

1. Directory introduction of systemctl service
Know that the management of service is through systemd, and most configuration files of systemd are placed in /usr/lib/systemd/the directory. However, the official Red Hat document pointed out that the files in this directory are mainly the settings provided by the original software, and it is recommended not to modify them! The location to be modified should be placed in /etc/systemd/system/the directory.

View details: https://wizardforcel.gitbooks.io/vbird-linux-basic-4e/content/150.html

Centos system service script directory:

/usr/lib/systemd/

There are system (system) and user (user). If you need to start a program that can be run without logging in, it exists in the system service (system), namely:

/usr/lib/systemd/system/

Conversely, programs that can only be run after the user logs in are stored in the user (user), and the service ends with .service.

/usr/lib/systemd/user/

2. Create kibana boot service
1), create kibana service file

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

Script content:

[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   

Note ⚠️:
The command here ExecStart、ExecReload、ExecStopis still implemented by configuring the kibana script in the /etc/init.d directory above.
[Service] start, restart, and stop commands all require the use of absolute paths
[Install] service installation related settings, which can be set to multi-user

Parameter description:
Description: Describe the service
After: Describe the service category

[Service] The setting of service operation parameters
Type=forking is the form of running in the background
User Service start user
Group Service start user group
ExecStart is the specific operation command of the service
ExecReload is the restart command
ExecStop is the stop command
PrivateTmp=True means assigning an independent temporary service to the service space

2) Give execution permission

chmod 754 kibana.service

According to the above table, the permission combination is the sum of the corresponding permission values, as follows:
7 = 4 + 2 + 1 read and write permission
5 = 4 + 1 read and run permission
4 = 4 read-only permission.
This command means to The read, write and run permissions of the filename file are given to the file owner, the read and run permissions are given to group users, and the read permissions are given to other users.

3), service start, stop, boot start

  • View service status
//重新加载某个服务的配置文件,如果新安装了一个服务,归属于 systemctl 管理,要是新服务的服务程序配置文件生效,需重新加载
systemctl daemon-reload
//查看服务状态
systemctl status kibana.service

insert image description here

  • Start the service
    insert image description here
    Among them, disabledit means that the service has not started to start automatically.

  • Start the service start

systemctl enable kibana.service

insert image description here

Description of service status:
systemctl status 服务名称

state illustrate
loaded The system service has been initialized and the configuration has been loaded
active(running) One or more programs are being executed in the system, vsftpd is this mode
atcive(exited) A service that ends normally after being executed only once, and currently there is no program executing in the system
atcive(waiting) Executing, but waiting for other events to continue processing
inactive service closed
enbaled service start
disabled Service does not start automatically
static Service startup items cannot be managed
failed System misconfiguration

Common service file
1.nginx.service

[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target

2.mysql.service

[Unit]
Description=mysql
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
#ExecReload=/usr/local/mysql/support-files/mysql.server restart
#ExecStop=/usr/local/mysql/support-files/mysql.server stop
#PrivateTmp=true

[Install]
WantedBy=multi-user.target

4.redis.service

[Unit]
Description=Redis
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /etc/redis.conf
ExecStop=kill -INT `cat /tmp/redis.pid`
User=www
Group=www

[Install]
WantedBy=multi-user.target

5.supervisord.service

[Unit]
Description=Process Monitoring and Control Daemon
After=rc-local.service

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf
SysVStartPriority=99

[Install]
WantedBy=multi-user.target

Summarize

This article mainly summarizes the three ways to configure boot self-start on Centos

  • Method 1: Add the service startup command directly in /etc/rc.d/rc.local
  • Method 2: Configure the service to start automatically through chkconfig
  • Method 3: Centos7 configures the service through systemctl enble to start automatically

Brother Bird's Linux private kitchen,
the most detailed CentOS7 setting custom boot service tutorial

Guess you like

Origin blog.csdn.net/w1014074794/article/details/127297409