redis開機自啟動的方法:1、打開終端;2、輸入命令編寫腳本;3、將代碼復制到腳本中;4,、輸入命令保存退出,設置權限;5、輸入命令啟動redis即可。
具體操作步驟:
1、首先打開終端命令行模式。
2、輸入以下命令編寫腳本。
vi /etc/init.d/redis
3、將以下代碼復制到腳本中。
#!/bin/sh
# chkconfig: 2345 10 90
# description: Start and Stop redis
REDISPORT=6379
EXEC=/usr/redis/redis-3.2.4/src/redis-server
CLIEXEC=/usr/redis/redis-3.2.4/src/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/redis/redis-3.2.4/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 ..."
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart)
"$0" stop
sleep 3
"$0" start
;;
*)
echo "Please use start or stop or restart as first argument"
;;
esac
4、輸入以下命令保存退出并設置權限。
chmod 777 /etc/init.d/redis
5、輸入以下命令啟動redis服務即可。
service redis start
相關操作命令:
/usr/local/bin/redis-server #啟動redis服務
/home/data/redis-3.2.1/redis.conf #啟動redis服務
./redis-cli -p 6379 shutdown #關閉redis服務,-p參數指定端口號