Redis設置systemctl管理服務的啓動、停止、開機啓動

Redis默認不會像mysql一樣開機自啓,故通過systemctl可以管理redis實現自啓

通過查找資料,發現這位的教程是好用的——原文鏈接: https://www.cnblogs.com/mr-wuxiansheng/p/12494124.html


1. 創建服務
用service來管理服務的時候,是在/etc/init.d/目錄中創建一個腳本文件,來管理服務的啓動和停止;

在systemctl中,也類似,文件目錄有所不同,在/lib/systemd/system目錄下創建一個腳本文件redis.service,裏面的內容如下:

 

[Unit]
Description=Redis
After=network.target

[Service]
ExecStart=/usr/local/bin/redis-server /usr/local/redis/redis.conf --daemonize no
ExecStop=/usr/local/bin/redis-cli -h 127.0.0.1 -p 6379 shutdown

[Install]
WantedBy=multi-user.target
[Unit] 表示這是基礎信息
Description 是描述
After 是在那個服務後面啓動,一般是網絡服務啓動後啓動
[Service] 表示這裏是服務信息
ExecStart 是啓動服務的命令
ExecStop 是停止服務的指令
[Install] 表示這是是安裝相關信息
WantedBy 是以哪種方式啓動:multi-user.target表明當系統以多用戶方式(默認的運行級別)啓動時,這個服務需要被自動運行。

 

2. 創建軟鏈接
創建軟鏈接是爲了下一步系統初始化時自動啓動服務

ln -s /lib/systemd/system/redis.service /etc/systemd/system/multi-user.target.wants/redis.service

創建軟鏈接就好比Windows下的快捷方式

ln -s 是創建軟鏈接
ln -s 原文件 目標文件(快捷方式的決定地址)

如果創建軟連接的時候出現異常,不要擔心,看看/etc/systemd/system/multi-user.target.wants/ 目錄是否正常創建軟鏈接爲準,有時候報錯只是提示一下,其實成功了。

$ ll /etc/systemd/system/multi-user.target.wants/
total 8
drwxr-xr-x 2 root root 4096 Mar 30 15:46 ./
drwxr-xr-x 13 root root 4096 Mar 13 14:18 ../
lrwxrwxrwx 1 root root 31 Nov 23 14:43 redis.service -> /lib/systemd/system/redis.service

 

3. 刷新配置
剛剛配置的服務需要讓systemctl能識別,就必須刷新配置

$ systemctl daemon-reload

 

如果沒有權限可以使用sudo

$ sudo systemctl daemon-reload

4. 啓動、重啓、停止
啓動redis

$ systemctl start redis

重啓redis

$ systemctl restart redis

停止redis

$ systemctl stop redis

5. 開機自啓動
redis服務加入開機啓動

$ systemctl enable redis

禁止開機啓動

$ systemctl disable redis


6. 查看狀態
查看狀態

$ systemctl status redis

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章