Redis 配置为 Service 系统服务

在Linux中,将程序配置为服务后,就可以使用service命令对系统服务进行管理,如:start(启动)、stop(停止)、restart(重启)等。Redis安装后默认不会配置为系统服务,本文将介绍Redis配置为服务的方法。

  1. 复制redis_init_script文件
  2. 编辑redis文件
  3. 注册系统服务
  4. rc.localservice服务

1. 复制redis_init_script文件

utils/redis_init_script文件复制/etc/rc.d/init.d/目录,并重命名为redis

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">cp utils/redis_init_script /etc/rc.d/init.d/redis</pre>

注意:以上命令需要在Redis源代码的根目录执行。redis_init_script文件是Redis提供的初始化脚本,可以在Reids源码/utils目录下找到。

2. 编辑redis文件

编辑/etc/rc.d/init.d/redis文件:

在文件第二行添加:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"># chkconfig: 2345 80 90</pre>

并将以下行(在start节点下):

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">CONF</pre>

修改为:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">CONF &</pre>

扫描二维码关注公众号,回复: 5196114 查看本文章

注:&会使服务在后台运行,不添加此符号redis会显示在前台,并影响其它服务的启动。

确认安装目录

文件中以下两行,为Redis安装的默认目录。如果实际安装目录与下两行不符,要将其修改为实际安装目录。

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli </pre>

复制conf文件

文件中有如下一行:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">CONF="/etc/redis/${REDISPORT}.conf"</pre>

该行表示Redis配置文件redis.conf的位置,而${REDISPORT}表示.confRedis运行端口命名。

文件位置应与实际位置一致,如,安装Redis后,我将redis.conf文件放在了/etc/redis.conf位置,所以将其修改如下:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">CONF="/etc/redis.conf"</pre>

当然,你也可不修改这一行,这时你需要创建/etc/redis,并将配置文件复制到这个目录下。分别执行以下两条命令:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">mkdir /etc/redis
cp redis.conf /etc/redis/6379.conf</pre>

3. 注册系统服务

注册系统服务使用chkconfig命令。注册redis服务命令如下:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">chkconfig --add redis </pre>

启动redis服务:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">service redis start</pre>

配置完成,现在就可以通过service命令对redis服务进行管理了。

4. rc.localservice服务

在另一篇文章中,我使用rc.local的方式添加了Redis的开机启动。这时应当注意,应该首先删除rc.local中的Redis开机配置,并停止Redis,然后通过service命名启动redis服务。

配置到rc.d目录的系统服务,其启动优先级要高于rc.local级别的服务,用户根据可根据需要在二者之间灵活选择。

更多关“Linux程序启动优先级”请参考:简记Linux开机脚本的执行顺序与程序的运行级别

猜你喜欢

转载自blog.csdn.net/weixin_34019144/article/details/87386147