Cleverly use the install_server.sh script to serve the redis process


foreword

The previous section introduced how to install and start the redis service, but the default startup script is still very inconvenient to manage the service, and it is not easy to manage the service process. install_server.shThis article will introduce how to serve the Redis process through the tool script provided in the Redis installation file .


1. Why process service

  • If the unified service management command
    does not perform service management, the startup and shutdown of each service process on Centos are different, and it is inconvenient to set it as a background daemon process startup, but after service, you can use a unified set of commands :
    For example:
//以Centos6中service命令为例
//查看服务状态
service service.name status
//服务启动
service service.name start
//服务停止
service service.name stop
//服务重启
service service.name restart
  • It is convenient to manage the life cycle of services, such as booting, monitoring
chkconfig –-add xxx //把服务添加到chkconfig列表
chkconfig --del xxx //把服务从chkconfig列表中删除
chkconfig xxx on //开启开机自动启动
chkconfig xxx off //关闭开机自动启动
chkconfig --list //查看所有chklist中服务
chkconfig --list xxx 查看指定服务

Second, the way of service management in CentOS

  • Before Centos6, service + chkconfigcommands were used for service management.
  • After Centos7, it is recommended to use systemdmanagement services.
cp systemd-redis_server.service /etc/systemd/system/redis_6379.service
//重载服务配置
systemctl daemon-reload

//启动服务
systemctl start redis_6379.service

//关闭服务
systemctl stop redis_6379.service

//查看服务
systemctl list-unit-files |grep redis

//查看服务状态
systemctl status redis_6379.service

//服务开机自启动
systemctl enable redis_6379.service

//取消服务开机自启动
systemctl disable redis_6379.service

3. Use install_server.sh to serve the redis process

Environmental description

  • Centos7.0
  • Redis 7.0

1. Service + chkconfig mode

本质是通过脚本,在/etc/init.d/目录下生成了一个服务控制文件。

If you execute the install_server.sh script directly, the following prompt will appear:

cd /usr/local/redis-stable/utils
./install_server.sh

insert image description here
Note:
The current system seems to use systemd for service management, please refer to the sample service file in the current directory for configuration.

Solution:
View the script, comment out the relevant judgments of the systemd service, and use service + chkconfig to configure the service.
insert image description here
Execute the install_server.sh script again, and enter according to the prompts to complete the service configuration, automatically install the redis_6379 service, add it to start, and finally start the service.
insert image description here

cat /etc/init.d/redis_6379
insert image description here

2. Use systemd to manage services

In the /utils directory, we are provided with two systemd-redis service script samples, one
of which systemd-redis_server.serviceis a sample of a single service instance, and the other [email protected]is a sample of a multi-service instance configuration.
insert image description here
Check the systemd-redis_server.service file:
it needs to be set supervised systemd, and the value of TimeoutStartSecand is specified TimeoutStopSec.
insert image description here

Specific configuration steps:

  1. If you need systemd support, add parameters when compilingmake USE_SYSTEMD=yes
    insert image description here
yum install systemd-devel -y

make distclean

make USE_SYSTEMD=yes
  1. Copy the systemd-redis_server.service file to the /etc/systemd/system/ directory
//复制示例服务配置到/etc/systemd/system/目录下,文件名改为redis_6379.service,其中redis_6379是服务名称
cp systemd-redis_server.service /etc/systemd/system/redis_6379.service
  1. Modify the redis_6379.service configuration file
    vi /etc/systemd/system/redis_6379.service
[Unit]
Description=Redis data structure server
Documentation=https://redis.io/documentation
#Before=your_application.service another_example_application.service
#AssertPathExists=/var/lib/redis
Wants=network-online.target
After=network-online.target

[Service]
#ExecStart=/usr/local/bin/redis-server --supervised systemd --daemonize no
## Alternatively, have redis-server load a configuration file:
//启动命令,需要修改
ExecStart=/usr/local/bin/redis-server /etc/redis/6379.conf
//停止命令,需要修改
ExecStop=/usr/local/bin/redis-cli -p 6379 shutdown
LimitNOFILE=10032
NoNewPrivileges=yes
#OOMScoreAdjust=-900
#PrivateTmp=yes
Type=notify
//需要修改
TimeoutStartSec=90
//需要修改
TimeoutStopSec=90
UMask=0077
//建议设置
#User=redis
#Group=redis
//建议设置
WorkingDirectory=/var/lib/redis/6379

[Install]
WantedBy=multi-user.target
  1. Modify the 6379.conf configuration file
    mainly to change the supervised attribute in the redis configuration file to systemd.
    vi /etc/redis/6379.conf
supervised systemd
  1. Load and start the service
//重新加载systemd的服务配置
systemctl daemon-reload

systemctl start redis_6379.service
//服务开机启动
systemctl enable redis_6379.service
//查看服务
systemctl list-unit-files |grep redis_6379
//查看服务状态
systemctl status redis_6379.service
systemctl stop redis_6379.service

Guess you like

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