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;">EXECCONF</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;">EXECCONF &</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}表示.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開機腳本的執行順序與程序的運行級別

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容