【问题标题】:Cannot start redis server on Centos 7 with systemctl无法使用 systemctl 在 Centos 7 上启动 redis 服务器
【发布时间】:2019-02-08 02:36:21
【问题描述】:

我在使用 systemctl 在 CentOS7 上启动 Redis 时遇到了一些麻烦。我应该怎么做才能解决问题?

我可以使用正常的命令来启动 Redis。喜欢:

# /etc/init.d/redis start 要么 /usr/local/bin/redis-server /etc/redis/config.conf

这是我放入/lib/systemd/system的redis.service文件:

[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
Type=forking
PIDFILE=/var/run/redis_6379.pid
ExecStart=/etc/init.d/redis start
ExecStop=/etc/init.d/redis stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

但是当我使用命令systemctl start redis 启动redis 服务器时。我什么都没有。

我尝试使用systemctl status redis 读取 systemctl 日志,它显示了这些消息:

● redis.service - Redis persistent key-value database
   Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled)
   Active: active (exited) since Fri 2018-08-31 15:45:37 CST; 2 days ago

Aug 31 15:45:37 redisserver001 systemd[1]: Starting LSB: start and stop redis_6379...
Aug 31 15:45:37 redisserver001 systemd[1]: Started LSB: start and stop redis_6379.
Aug 31 15:45:37 redisserver001 redis[24755]: /var/run/redis_6379.pid exists, process is already running or crashed
Sep 03 10:31:21 redisserver001 systemd[1]: [/usr/lib/systemd/system/redis.service:6] Unknown lvalue 'PIDFILE' in section 'Service'
Sep 03 10:33:13 redisserver001 systemd[1]: [/usr/lib/systemd/system/redis.service:6] Unknown lvalue 'PIDFILE' in section 'Service'
Sep 03 10:45:32 redisserver001 systemd[1]: [/usr/lib/systemd/system/redis.service:7] Unknown lvalue 'PIDFILE' in section 'Service'
Sep 03 11:08:28 redisserver001 systemd[1]: [/usr/lib/systemd/system/redis.service:7] Unknown lvalue 'PIDFILE' in section 'Service'

以下项目是我认为可能影响redis运行的关键配置。但我不知道我在哪里犯了错误。请帮忙。非常感谢。

pidfile /var/run/redis_6379.pid
daemonize yes
supervised systemd

【问题讨论】:

    标签: redis centos7 systemctl


    【解决方案1】:

    如果应用程序在服务文件中指定“pidfile”属性,则应用程序有责任在服务初始化完成之前将主进程的 pid 写入该文件。您需要确保您的应用程序正在执行此操作。 Systemd 会读取这个值,如果用户执行“systemctl start”,并且 pid 文件已经存在,系统会阻止创建另一个分叉进程。从您发布的输出来看,systemd 似乎认为 redis 进程已经在运行(因为存在 pid 文件,并且没有创建新的)。您可以在服务文件的“ExecStartPost”子句中设置 pid。比如:

    ExecStartPost=/bin/sh -c 'umask 022; pgrep YOURSERVICE > /var/run/YOURSERVICE.pid'
    

    【讨论】:

    • 感谢您的回复。我试图删除redis.service 文件中的PIDFILE 设置。但是systemctl仍然无法启动服务。
    • 您是否也尝试删除文件 /var/run/pid_8579.pid ?旧的工件可能会阻止您的进程启动。
    • 是的。我也尝试删除/var/run/redis_6379.pid。还是不行。
    • 你可以在删除pid文件后尝试启动服务器后发布“systemctl status redis”的输出吗?
    • 当我在删除 pid 文件后运行 systemctl status redis 时,输出没有为我添加任何新消息。但我想我可能会发现这个问题的真正问题。当我杀死 redis 进程时,我无法使用systemctl start redis 再次启动 redis,即使我删除了 pid 文件。但我可以使用systemctl restart redis 来提出它。如果我像往常一样使用systemctl stop redis 停止redis 服务,那么systemctl start redis 将正常工作。太诡异了。
    【解决方案2】:

    选项必须是PIDFile(区分大小写)。来自手册页man systemd.service

    PIDFile=
      Takes a path referring to the PID file of the service. Usage of this option is recommended for
      services where Type= is set to forking. The path specified typically points to a file below /run/. If
      a relative path is specified it is hence prefixed with /run/. The service manager will read the PID
      of the main process of the service from this file after start-up of the service. The service manager
      will not write to the file configured here, although it will remove the file after the service has
      shut down if it still exists. The PID file does not need to be owned by a privileged user, but if it
      is owned by an unprivileged user additional safety restrictions are enforced: the file may not be a
      symlink to a file owned by a different user (neither directly nor indirectly), and the PID file must
      refer to a process already belonging to the service.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-29
      • 1970-01-01
      • 1970-01-01
      • 2020-07-12
      • 2015-07-22
      • 2016-05-31
      • 1970-01-01
      • 2015-02-24
      相关资源
      最近更新 更多