在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開機腳本的執行順序與程序的運行級別