在Linux中,将程序配置为服务后,就可以使用service
命令对系统服务进行管理,如:start
(启动)、stop
(停止)、restart
(重启)等。Redis
安装后默认不会配置为系统服务,本文将介绍Redis
配置为服务的方法。
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>
注:&
会使服务在后台运行,不添加此符号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}
表示.conf
以Redis
运行端口命名。
文件位置应与实际位置一致,如,安装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.local
与service
服务
在另一篇文章中,我使用rc.local
的方式添加了Redis
的开机启动。这时应当注意,应该首先删除rc.local
中的Redis
开机配置,并停止Redis
,然后通过service
命名启动redis
服务。
配置到rc.d
目录的系统服务,其启动优先级要高于rc.local
级别的服务,用户根据可根据需要在二者之间灵活选择。
更多关“Linux程序启动优先级”请参考:简记Linux开机脚本的执行顺序与程序的运行级别
网友评论