前言:
近期在调试新搭建的pinpoint2.3.3链路监控系统,频繁需要重启各个相关的服务,为了方便,捣鼓了一个服务启停的shell脚本。
脚本参考:https://blog.csdn.net/weixin_45600715/article/details/128966390
各服务之前启停脚本:
#1.pinpoint-collector
#启动命令:
cd /opt/pinpoint
nohup java -Dpinpoint.zookeeper.address=localhost -jar /opt/pinpoint/pinpoint-collector-boot-2.3.3.jar >/opt/pinpoint/logs/pinpoint-collector.log 2>&1 &
#停止命令:
ps -ef | grep pinpoint-collector-boot | grep -v grep | awk '{print $2}' | xargs kill -9#2.pinpoint-web
#启动命令:
cd /opt/pinpoint
nohup java -Dpinpoint.zookeeper.address=localhost -Dspring.config.additional-location=/opt/pinpoint/config/web.properties -jar /opt/pinpoint/pinpoint-web-boot-2.3.3.jar >/opt/pinpoint/logs/pinpoint-web.log 2>&1 &
#停止命令:
ps -ef | grep pinpoint-web-boot | grep -v grep | awk '{print $2}' | xargs kill -9#3.pinpoint-batch
#启动命令:
cd /opt/pinpoint
nohup java -Dspring.profiles.active=release -Dspring.config.location=/opt/pinpoint/config/pinpoint-batch.properties -jar /opt/pinpoint/pinpoint-batch-2.3.3.jar --server.port=8088 >/opt/pinpoint/logs/pinpoint-batch.log 2>&1 &
#停止命令:
ps -ef | grep pinpoint-batch | grep -v grep | awk '{print $2}' | xargs kill -9#4.飞书告警消息推送定时任务
slack-receiver:
#启动命令:
cd /opt/pinpoint/
nohup java -Dlog.dir=/opt/pinpoint/logs/ -Dspring.config.additional-location=/opt/pinpoint/config/slack.properties -jar /opt/pinpoint/slack-receiver-0.0.1-SNAPSHOT.jar >/opt/pinpoint/logs/slack-receiver.log 2>&1 &
#停止命令:
ps -ef | grep slack-receiver | grep -v grep | awk '{print $2}' | xargs kill -9
在/opt/pinpoint/目录下新增了service.sh
新的启停命令:
sh service.sh [start|stop|restart|status] [pinpoint-batch-2.3.3.jar|pinpoint-collector-boot-2.3.3.jar|pinpoint-web-boot-2.3.3.jar|slack-receiver-0.0.1-SNAPSHOT.jar]
相对应的启停脚本:
#!/bin/bash
# pinpoint2.3.3启动脚本,请放置和jar包相同目录
# 建议目录结构:/opt/pinpoint/***.jar /opt/pinpoint/service.sh /opt/pinpoint/config/ /opt/pinpoint/logs/
# 使用方式:sh service.sh [start|stop|restart|status] [pinpoint-batch-2.3.3.jar|pinpoint-collector-boot-2.3.3.jar|pinpoint-web-boot-2.3.3.jar|slack-receiver-0.0.1-SNAPSHOT.jar]
# 切换执行目录为service.sh脚本所在目录
cd $(dirname $0)
# 获取传入的第二个参数jar包名称
# INPUT:pinpoint-batch-2.3.3.jar|pinpoint-collector-boot-2.3.3.jar|pinpoint-web-boot-2.3.3.jar|slack-receiver-0.0.1-SNAPSHOT.jar
INPUT=$2
# JAR_PATH:/opt/pinpoint/service_name.jar
JAR_PATH=$(readlink -f ${INPUT})
# JAR_NAME:service_name.jar
JAR_NAME=${INPUT##*/}
# SERVICE_NAME:service_name
SERVICE_NAME=${JAR_NAME%.*}
# DEPLOY_DIR:/opt/pinpoint
DEPLOY_DIR=$(pwd)
# LOG_DIR:/opt/pinpoint/logs
LOG_DIR="$DEPLOY_DIR/logs"
# CONFIG_DIR:/opt/pinpoint/config
CONFIG_DIR="$DEPLOY_DIR/config"
PID=0
# 根据不同的服务拼接启动参数
START_OPTIONS=""
LOG_OPTIONS=""
OTHER_OPTIONS=""
if [[ "$JAR_NAME" == *"collector"* ]]; then
START_OPTIONS="-Dpinpoint.zookeeper.address=localhost"
LOG_OPTIONS="$LOG_DIR/pinpoint-collector.log"
elif [[ "$JAR_NAME" == *"web"* ]]; then
START_OPTIONS="-Dpinpoint.zookeeper.address=localhost -Dspring.config.additional-location=$CONFIG_DIR/web.properties"
LOG_OPTIONS="$LOG_DIR/pinpoint-web.log"
elif [[ "$JAR_NAME" == *"batch"* ]]; then
START_OPTIONS="-Dspring.profiles.active=release -Dspring.config.location=$CONFIG_DIR/pinpoint-batch.properties"
LOG_OPTIONS="$LOG_DIR/pinpoint-batch.log"
OTHER_OPTIONS="--server.port=8088"
elif [[ "$JAR_NAME" == *"slack"* ]]; then
START_OPTIONS="-Dlog.dir=$LOG_DIR -Dspring.config.additional-location=$CONFIG_DIR/slack.properties"
LOG_OPTIONS="$LOG_DIR/slack-receiver.log"
else
echo "执行命令参数有误,请检查!"
echo "执行命令参考:sh service.sh [start|stop|restart|status] [pinpoint-batch-2.3.3.jar|pinpoint-collector-boot-2.3.3.jar|pinpoint-web-boot-2.3.3.jar|slack-receiver-0.0.1-SNAPSHOT.jar]"
exit 0
fi
#检查程序是否在运行
is_exist() {
PID=$(ps -ef | grep $JAR_PATH | grep -v grep | awk '{print $2}')
#如果不存在返回1,存在返回0
if [ -z "${PID}" ]; then
return 1
else
return 0
fi
}
#启动方法
start() {
echo "==============================start=============================="
# 不存在临时文件目录,则创建
if [[ ! -d "$LOG_DIR" ]]; then
mkdir -p $LOG_DIR
fi
if [[ ! -d "$CONFIG_DIR" ]]; then
mkdir -p $CONFIG_DIR
fi
is_exist
if [ $? -eq "0" ]; then
echo "--- Service $SERVICE_NAME is already running, PID is $PID ---"
else
echo "启动命令:nohup java $START_OPTIONS -jar $JAR_PATH $OTHER_OPTIONS >$LOG_OPTIONS 2>&1 &"
nohup java $START_OPTIONS -jar $JAR_PATH $OTHER_OPTIONS >$LOG_OPTIONS 2>&1 &
PID=$!
if [[ -n $PID ]]; then
echo "--- Start $SERVICE_NAME successfully, PID is $PID ---"
else
echo "--- Failed to start $SERVICE_NAME!!! ---"
fi
fi
echo "==============================start=============================="
}
#停止方法
stop() {
echo "==============================stop=============================="
is_exist
if [ $? -eq "0" ]; then
echo "--- Begin kill -15 $SERVICE_NAME, PID is $PID ---"
kill -15 $PID
sleep 5
is_exist
if [ $? -eq "0" ]; then
echo "--- Stop $SERVICE_NAME failed by kill -15 $PID, begin to kill -9 $PID ---"
kill -9 $PID
sleep 2
echo "--- Stop $SERVICE_NAME successfully by kill -9 $PID ---"
else
echo "--- Stop $SERVICE_NAME successfully by kill -15 $PID ---"
fi
else
echo "--- $SERVICE_NAME is not running!!! ---"
fi
echo "==============================stop=============================="
}
#输出运行状态
status() {
echo "==============================status=============================="
is_exist
if [ $? -eq "0" ]; then
echo "--- $SERVICE_NAME is running, PID is $PID ---"
else
echo "--- $SERVICE_NAME is not running!!! ---"
fi
echo "==============================status=============================="
}
#重启
restart() {
echo "==============================restart=============================="
stop
sleep 2
start
echo "==============================restart=============================="
}
#根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
esac
exit 0