如何用systemd管理程序【redis 6为例】

说明:
实现用systemctl管理应用程序

安装包目录结构:

└── redis
    ├── install.sh
    ├── readme.txt
    ├── redis-cli
    ├── redis.conf
    ├── redis-server
    └── redis.service

下面这三个是编译 redis 6.2.6 所得,在源码src目录下

    ├── redis-cli
    ├── redis.conf
    ├── redis-server

源码地址:https://github.com/redis/redis/archive/refs/tags/6.2.6.tar.gz

编译:

CentOS下编译使用systemd先安装 systemd-devel:yum install systemd-devel
然后: make USE_SYSTEMD=yes
然后: 从src目录中摘取出来

install.sh:

#!/bin/sh
d=`date "+%Y%m%d%S"`
InstallDir="/usr/local/redis"
chmod +x redis-server

# check for root user
if [ "$(id -u)" -ne 0 ] ; then
        echo "You must run this script as root. Sorry!"
        exit 1
fi


# save old
if [ -d $InstallDir ];then
    echo "save old..."
    mv $InstallDir $InstallDir.backup.$d
fi


# install
echo "install..."
mkdir $InstallDir
mv ./* $InstallDir
cd $InstallDir
mv redis.service /etc/systemd/system/redis.service


# clean
echo "clean.."
if [ -f dump.rdb ];then
    rm -rf dump.rdb
fi
rm -rf install.sh
cd - && cd .. && rm -rf redis


# init
echo "init.."
systemctl daemon-reload
systemctl start redis
systemctl enable redis


# done
echo
echo "redis 6.2.6 installed : $InstallDir"

redis.service:

[unit]
Description=redis-server
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/redis/redis-server /usr/local/redis/redis.conf
ExecStop=/usr/local/redis/redis-cli -a "Pe2695#7381#2019" shutdown
PrivateTmp=true
Restart=always
RestartSec=1

[Install]
WantedBy=multi-user.target

readme.txt

service of Redis 6.2.6

start:   systemctl start redis
stop:    systemctl stop redis
restart: systemctl restart redis

然后压缩:

tar -zcvf redisInstall.tar.gz redis

使用(不要放到/usr/local目录安装):

tar -xvf redisInstall.tar.gz
cd redis
./install.sh

结果:
设置了systemctl管理,安装程序到 /usr/local/redis,查看状态如下,然后就是关注配置了

[root@localhost redisInstallPackageMake]# systemctl status redis
● redis.service
   Loaded: loaded (/etc/systemd/system/redis.service; enabled; vendor preset: disabled)
   Active: active (running) since 一 2021-10-18 21:10:01 CST; 17s ago
 Main PID: 354333 (redis-server)
   CGroup: /system.slice/redis.service
           └─354333 /usr/local/redis/redis-server 127.0.0.1:6379

1018 21:10:01 localhost.localdomain redis-server[354333]: 354333:M 18 Oct 2021 21:10:01.497 * Running mode=standalone, port=6379.
1018 21:10:01 localhost.localdomain redis-server[354333]: 354333:M 18 Oct 2021 21:10:01.497 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net...ue of 128.
1018 21:10:01 localhost.localdomain redis-server[354333]: 354333:M 18 Oct 2021 21:10:01.497 # Server initialized
1018 21:10:01 localhost.localdomain redis-server[354333]: 354333:M 18 Oct 2021 21:10:01.497 # WARNING overcommit_memory is set to 0! Background save may fail under low memory...ke effect.
1018 21:10:01 localhost.localdomain redis-server[354333]: 354333:M 18 Oct 2021 21:10:01.497 * Loading RDB produced by version 6.2.6
1018 21:10:01 localhost.localdomain redis-server[354333]: 354333:M 18 Oct 2021 21:10:01.498 * RDB age 0 seconds
1018 21:10:01 localhost.localdomain redis-server[354333]: 354333:M 18 Oct 2021 21:10:01.498 * RDB memory usage when created 0.77 Mb
1018 21:10:01 localhost.localdomain redis-server[354333]: 354333:M 18 Oct 2021 21:10:01.498 # Done loading RDB, keys loaded: 3, keys expired: 0.
1018 21:10:01 localhost.localdomain redis-server[354333]: 354333:M 18 Oct 2021 21:10:01.498 * DB loaded from disk: 0.000 seconds
1018 21:10:01 localhost.localdomain redis-server[354333]: 354333:M 18 Oct 2021 21:10:01.498 * Ready to accept connections
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost redisInstallPackageMake]#

原创内容,转载请附带原文地址:https://blog.csdn.net/weixin_44328568/article/details/120834549。

Guess you like

Origin blog.csdn.net/weixin_44328568/article/details/120834549