服务器上有一些应用,启动步骤比较繁琐,需要依次启动多个服务。服务之间存在依赖关系。例如:必须服务1启动成功才能启动服务2。现将启动服务的步骤写成了sh脚本,大家可以参考这个脚本写一个自己服务的启动脚本。本文主要以Karfka和RocketMQ为案例。
Karfka需要依次启动的服务有:Zookeeper,Kafka,Eagle。在启动上下一个服务时会判断上一个服务是否启动成功。
参考脚本如下:
#!/bin/bash
# 启动Zookeeper
cd /www/wwwroot/install/apache-zookeeper-3.7.1-bin/bin
# 检查Zookeeper是否启动成功
if ./zkServer.sh status | grep "Mode: "; then
echo "Zookeeper is started.";
else
echo "Zookeeper is Starting ...";
./zkServer.sh start zoo.cfg
while true; do
if ./zkServer.sh status | grep "Mode: "; then
echo "Zookeeper started successfully.";
break
else
sleep 1
fi
done
fi
# 启动Kafka
cd /www/wwwroot/install/kafka_2.12-3.5.0
# 检查Zookeeper是否启动成功
if ./bin/kafka-topics.sh --list --bootstrap-server dev-study:9092 >/dev/null 2>&1; then
echo "Kafka is started.";
else
echo "Kafka is starting...";
./bin/kafka-server-start.sh -daemon config/server.properties
while true; do
if ./bin/kafka-topics.sh --list --bootstrap-server dev-study:9092 >/dev/null 2>&1; then
echo "Kafka started successfully.";
break
else
sleep 1
fi
done
fi
# 启动Kafka Eagle
cd /www/wwwroot/install/kafka-eagle
# 检查Zookeeper是否启动成功
if bin/ke.sh status | grep "is running"; then
echo "Eagle is started.";
else
echo "Eagle is starting...";
./bin/ke.sh start
while true; do
if bin/ke.sh status | grep "is running"; then
echo "Eagle started successfully.";
break
else
sleep 1
fi
done
fi
echo "All services started successfully.";
RocketMQ需要依次启动的服务有:nameserver,broker,dashboard。在启动上下一个服务时会判断上一个服务是否启动成功。(这些服务如何安装?)
参考脚本如下:
#!/bin/bash
#软件安装目录设置,先读环境变量,读不到则用默认值
ROCKETMQ_HOME=$ROCKETMQ_HOME
if [ -z $ROCKETMQ_HOME]; then
echo "ROCKETMQ_HOME is not set! the program will use default value"
ROCKETMQ_HOME=/www/wwwroot/install/rocketmq-all-4.9.7-bin-release
fi
echo "ROCKETMQ_HOME=$ROCKETMQ_HOME"
# 启动rocketmq nameserver
cd ${ROCKETMQ_HOME}/bin
# 检查nameserver是否启动成功
if jps -ml | grep "namesrv.NamesrvStartup"; then
echo "rocketmq nameserver is running.";
else
echo "rocketmq nameserver is Starting ...";
rm -rf nohup.out;
nohup sh mqnamesrv &
count=0
while [ $count -le 10 ]; do
if tail -100 nohup.out | grep "Name Server boot success"; then
echo "rocketmq nameserver has been started successfully";
break
else
count=$((count+1))
sleep 2
fi
done
fi
# 启动rocketmq broker
# 检查broker是否启动成功
if jps -ml | grep "org.apache.rocketmq.broker.BrokerStartup"; then
echo "rocketmq broker is running.";
else
echo "rocketmq broker is Starting ...";
rm -rf nohup.out;
nohup sh mqbroker -c ../conf/broker.conf -n dev-study:9876 autoCreateTopicEnable=true &
count=0
while [ $count -le 10 ]; do
if tail -100 nohup.out | grep "broker.*success"; then
echo "rocketmq broker has been started successfully";
break
else
count=$((count+1))
sleep 2
fi
done
fi
# 启动rocketmq dashboard
cd ${ROCKETMQ_HOME}/dashboard
if jps -ml | grep "rocketmq-dashboard"; then
echo "rocketmq dashboard is running.";
else
echo "rocketmq dashboard is Starting ...";
rm -rf nohup.out;
nohup java -jar rocketmq-dashboard-1.0.0.jar &
count=0
while [ $count -le 10 ]; do
if tail -100 nohup.out | grep "Tomcat started on port(s)"; then
echo "rocketmq dashboard has been started successfully";
break
else
count=$((count+1))
sleep 2
fi
done
fi
echo "All services started.";