redis-7.4.2 通过 systemd管理,rpmbuild spec文件参考
redis-7 和 redis 5 版本在配置为systemd 方式管理时,配置关于有些许区别,否则会报systemctl status redis 如下错误:
redis.service: control process exited, code=exited status=1 Failed to
start Redis persistent key-value database. Unit redis.service entered
failed state.
这类报错很多没什么参考意义,可以尝试通过启动日志等排查具体问题,
journalctl -xe查看报错如下:
– Unit redis.service has begun starting up.
May 22 18:11:54 worker1 systemd[1]: Failed to start Redis persistent key-value database.
– Subject: Unit redis.service has failed
– Defined-By: systemd
– Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
–
– Unit redis.service has failed.
–
– The result is failed.
May 22 18:11:54 worker1 systemd[1]: Unit redis.service entered failed state.
May 22 18:11:54 worker1 systemd[1]: redis.service failed.
May 22 18:11:54 worker1 polkitd[795]: Unregistered Authentication Agent for unix-process:103583:240646911 (system bus name :1.17442, object path /org/freedesktop/PolicyKit1/A
正确的配置如下:
/usr/lib/systemd/system/redis.service
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target[Service]
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf
ExecStop=/usr/libexec/redis-shutdown
Restart=on-failure
Type=simple #注意这里为simple即可,旧版本为notify
User=root
Group=root
RuntimeDirectory=redis
RuntimeDirectoryMode=0755[Install]
WantedBy=multi-user.target
配置文件
关键信息如下
daemonize no #配置说明里面虽然说当supervised 为systemd的这里的设置不影响,但实测必须为no,为yes则自动报错
supervised systemd #显示指定管理方式为systemd
可将redis打包为rpm方便安装,spec文件如下:
redis-7.4.2.spec
Name: redis
Version: 7.4.2
Release: 1%{?dist}
Summary: A persistent key-value databaseGroup: Applications/Databases
License: BSD
URL: http://redis.io
Source0: %{name}-%{version}.tar.gz
Source1: redis-7.conf
Source2: redis-7.service
Source3: redis-shutdown
BuildRoot: %{_tmppath}/%{name}-%{version}
BuildRequires: tcl >= 8.5
ExcludeArch: ppc64
#Requires:%description
Redis is an advanced key-value store. It is similar to memcached but the data
set is not volatile, and values can be strings, exactly like in memcached, but
also lists, sets, and ordered sets. All this data types can be manipulated with
atomic operations to push/pop elements, add/remove elements, perform server side
union, intersection, difference between sets, and so forth. Redis supports
different kind of sorting abilities.%prep
%setup -q%build
make%install
rm -fr %{buildroot}
make PREFIX=%{buildroot}/home/software/redis install
mkdir -p %{buildroot}/home/software/redis/log
install -p -D -m 644 %{SOURCE1} %{buildroot}/home/software/redis/conf/%{name}.conf
install -p -D -m 644 %{SOURCE2} %{buildroot}/usr/lib/systemd/system/redis.service
install -p -D -m 755 %{SOURCE3} %{buildroot}/usr/libexec/redis-shutdown%pre%post
if [ $1 -eq 1 ];then# 第一次安装时启用服务ln -s /home/software/redis /usr/local/redisln -s /usr/local/redis/bin/redis-cli /usr/local/bin/redis-cli/bin/systemctl daemon-reload/bin/systemctl enable %{name}.service >/dev/null 2>&1 || :
fi%preun
if [ $1 -eq 0 ]; then/bin/systemctl disable %{name}.service >/dev/null 2>&1 || :/bin/systemctl stop %{name}.service >/dev/null 2>&1 || :
fi%postunrm -f /usr/local/bin/redis-clirm -f /usr/local/redisrm -fr /home/software/redisrm -f /usr/libexec/redis-shutdownrm -f /usr/lib/systemd/system/redis.service%files
/home/software/redis
/usr/lib/systemd/system/redis.service
/usr/libexec/redis-shutdown%clean
rm -fr %{buildroot}%changelog
* Thu May 22 2025 Rockstics <rockstics@163.com> - 7.4.2-1
- Initial package creation for Redis 7.4.2