redis单机设置开机自启
时间: 2023-09-28 12:06:55 浏览: 57
要在Redis单机上设置开机自启动,可以按照以下步骤进行操作:
1. 打开终端并切换到root用户或具有sudo权限的用户。
2. 编辑Redis的启动脚本。在大多数Linux系统上,Redis的启动脚本位于`/etc/init.d/redis`目录下,请根据你的系统自行确认路径。
```shell
sudo vi /etc/init.d/redis
```
3. 在启动脚本中添加以下内容:
```shell
#!/bin/sh
# Redis startup script
REDIS_USER=<redis_user> # Redis所属的用户
REDIS_PATH=<redis_path> # Redis安装目录
case "$1" in
start)
echo "Starting Redis..."
su -s /bin/sh -c "$REDIS_PATH/src/redis-server $REDIS_PATH/redis.conf" $REDIS_USER
;;
stop)
echo "Stopping Redis..."
su -s /bin/sh -c "pkill redis-server" $REDIS_USER
;;
*)
echo "Usage: /etc/init.d/redis {start|stop}"
exit 1
;;
esac
exit 0
```
请将`<redis_user>`替换为Redis所属的用户,将`<redis_path>`替换为Redis的安装目录。
4. 保存并退出编辑器。
5. 授予启动脚本执行权限:
```shell
sudo chmod +x /etc/init.d/redis
```
6. 在系统启动时自动运行Redis,可以使用以下命令将Redis服务添加到启动项中:
```shell
sudo update-rc.d redis defaults
```
如果你的系统不支持`update-rc.d`命令,你可以尝试使用`chkconfig`命令(适用于CentOS/RHEL等系统):
```shell
sudo chkconfig --add redis
```
现在,当你的系统启动时,Redis将自动启动。你可以使用以下命令手动启动和停止Redis:
```shell
sudo /etc/init.d/redis start # 启动Redis服务
sudo /etc/init.d/redis stop # 停止Redis服务
```
希望能对你有所帮助!如果还有其他问题,请随时提问。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)