1,进入/etc/init.d目录
cd /etc/init.d
2,创建自启动脚本redis和zookeeper
vim zookeeper
##添加脚本内容
vim redis
##添加脚本内容
3,复制下面脚本内容到对应脚本文件
4,为脚本文件添加执行权限
chmod +x zookeeper
chmod +x redis
5,配置开机启动
chkconfig --add zookeeper
chkconfig --add redis
Zookeeper脚本内容
#!/bin/bash
#chkconfig:2345 20 90
#description:zookeeper
#processname:zookeeper
#这里设置你的Java_home的路径
export JAVA_HOME=/usr/local/jdk1.8.0_221
case $1 in
start) su root /usr/local/zookeeper-3.4.6/bin/zkServer.sh start;;
stop) su root /usr/local/zookeeper-3.4.6/bin/zkServer.sh stop;;
status) su root /usr/local/zookeeper-3.4.6/bin/zkServer.sh status;;
restart) su root /usr/local/zookeeper-3.4.6/bin/zkServer.sh restart;;
*) echo "require start|stop|status|restart" ;;
esac
Redis脚本内容
#!/bin/bash
#chkconfig: 22345 10 90
#description: Start and Stop redis
REDISPORT=6379
#这里配置你的redis-server目录
EXEC=/usr/local/redis/bin/redis-server
#这里配置你的redis-cli客户端目录
CLIEXEC=/usr/local/redis/bin/redis-cli
PIDFILE=/var/run/redis_6379.pid
#这里配置你的redis配置文件目录
CONF="/usr/local/redis/bin/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