ubuntu redis开机自启动
时间: 2023-11-13 11:57:21 浏览: 248
可以通过以下步骤实现 Ubuntu 下 Redis 的开机自启动:
1. 编辑 Redis 的配置文件 `/etc/redis/redis.conf`,将其中的 `daemonize no` 改为 `daemonize yes`,使 Redis 以守护进程的方式运行。
2. 创建 Redis 的启动脚本 `/etc/init.d/redis`,并将以下内容复制到文件中:
```
#!/bin/sh
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/redis.conf"
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping Redis server..."
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
*)
echo "Please use start or stop as first argument"
;;
esac
exit 0
```
3. 将 Redis 启动脚本添加到系统服务中,并设置开机自启动:
```
sudo chmod +x /etc/init.d/redis
sudo update-rc.d redis defaults
```
现在,Redis 就会在 Ubuntu 启动时自动启动了。
阅读全文