#!/bin/bash
#************************************************************************************
#*
#*A script edited for operating the jar file under the current path.
#*author bluenfish 2022-12-02
#*
#*注意变量赋值时, "="两边不要给空格, 否则command not found;
#*注意变量是命令时, 有"-x"参数,参数和命令的两边都建议有空格, 否则command not found;
#************************************************************************************
echo -e "\033[0;32m Start executing script $0 \033[0m"
#手动配置项
#which java
JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.302.b08-0.el7_9.x86_64
#启动参数
JAVA_OPTS=" "
#上传到服务器jar包的临时路径
TEMP_JAR_PATH=../tempJars
TARGET_PORT=8021
echo -e "\033[0;32m 手动配置项 JAVA_HOME:$JAVA_HOME JAVA_OPTS:$JAVA_OPTS TEMP_JAR_PATH:$TEMP_JAR_PATH TARGET_PORT:$TARGET_PORT \033[0m"
JAR_NAME=$2
CHECK_TARGET_PORT=$3
echo -e "\033[0;32m \$1:$1 \$2:$2 \033[0m"
# "[]"里面的"="的两边要有空格
if [ "$1" = "" ];
then
echo -e "\033[0;31m 未输入操作名 \033[0m \033[0;32m {bakrestart|restart|start|stop|status|mvtempjar|bakrestart-p|restart-p|start-p|stop-p|staus-p} \033[0m"
exit 1
fi
if [ "$1" = "start-p" -o "$1" = "stop-p" -o "$1" = "restart-p" -o "$1" = "bakrestart-p" -o "$1" = "status-p" ];
then
if [ "$TARGET_PORT" = "" ];
then
echo -e "\033[0;31m 未输入目标端口 \033[0m"
exit 1
fi
if [ "$TARGET_PORT" != "$CHECK_TARGET_PORT" ];
then
echo -e "\033[0;31m 配置端口与输入端口不一致 \033[0m"
exit 1
fi
if [ "$JAVA_OPTS" = "" ];
then
$JAVA_OPTS=" -Dserver.port=$TARGET_PORT "
echo -e "\033[0;32m 启动参数校准为: $JAVA_OPTS \033[0m"
fi
fi
if [ "$JAR_NAME" = "" ];
then
echo -e "\033[0;31m 未指定jar包 \033[0m"
exit 1
fi
if [ ! -f "./$JAR_NAME" ];
then
echo -e "\033[0;31m ./$JAR_NAME is not exists. \033[0m"
# 按顺序
if [ "$1" = "bakrestart" -o "$1" = "bakrestart-p" ];
then
echo -e "\033[0;31m $1操作需要./$JAR_NAME存在 \033[0m"
exit 1
elif [ "$1" = "restart" -o "$1" = "restart-p" ];
then
echo -e "\033[0;31m $1操作需要./$JAR_NAME存在 \033[0m"
exit 1
elif [ "$1" = "start" -o "$1" = "start-p" ];
then
echo -e "\033[0;31m $1操作需要./$JAR_NAME存在 \033[0m"
exit 1
elif [ "$1" = "mvtempjar" ];
then
echo -e "\033[0;31m $1操作需要./$JAR_NAME存在 \033[0m"
exit 1
else
echo -e "\033[0;32m 继续向下执行 \033[0m"
fi
else
echo -e "\033[0;32m ./$JAR_NAME is exists. \033[0m"
fi
function status()
{
show=`ps -ef | grep java | grep $JAR_NAME | grep -v grep`
if [ "$show" = "" ]; then
echo -e "\033[0;31m No application is running by $JAR_NAME \033[0m"
fi
echo -e "\033[0;32m $show \033[0m"
}
function status_p()
{
show=`lsof -i:$TARGET_PORT|grep -v ESTABLISHED | grep -v PID`
if [ "$show" = "" ];
then
echo -e "\033[0;31m No application is running on port $TARGET_PORT \033[0m"
fi
echo -e "\033[0;32m $show \033[0m"
}
function stop()
{
pid=`ps -ef |grep java|grep $JAR_NAME|grep -v grep|awk '{print $2}'`
count=`ps -ef |grep java|grep $JAR_NAME|grep -v grep|wc -l`
if [ ! -z "$pid" ] && [ "$count" = "1" ]; then
kill -9 $pid
echo -e "\033[0;32m $pid has been killed forcibly! \033[0m"
else
status
fi
}
##lsof -i:8000|grep -v PID|awk '{print $2}'|xargs -r kill -9
function stop_p()
{
pid=`lsof -i:$TARGET_PORT|grep -v ESTABLISHED | grep -v PID|awk '{print $2}'`
if [ ! -z "$pid" ]; then
kill -9 $pid
echo -e "\033[0;32m pid=$pid has been killed forcibly! \033[0m"
else
status
fi
}
function start()
{
sleep 1s
count=`ps -ef |grep java|grep $JAR_NAME|grep -v grep|wc -l`
if [ $count != 0 ];then
status
echo -e "\033[0;31m $JAR_NAME is running... \033[0m"
exit 1
else
nohup $JAVA_HOME/bin/java $JAVA_OPTS -jar $JAR_NAME >> ./nohup.out 2>&1 &
sleep 2s
tail -f ./nohup.out
fi
}
function start_p()
{
sleep 1s
pid=`lsof -i:$TARGET_PORT|grep -v ESTABLISHED | grep -v PID|awk '{print $2}'`
if [ "$pid" = "" ]; then
nohup $JAVA_HOME/bin/java $JAVA_OPTS -jar $JAR_NAME >> ./nohup.out 2>&1 &
sleep 2s
tail -f ./nohup.out
else
status
echo -e "\033[0;31m $TARGET_PORT is already in use. \033[0m"
exit 1
fi
}
function mvtempjar()
{
if [ -f "./$JAR_NAME" ]; then
echo -e "\033[0;31m ./$JAR_NAME has exists. \033[0m"
elif [ -d "$TEMP_JAR_PATH" -a -f "$TEMP_JAR_PATH/$JAR_NAME" ]; then
mv $TEMP_JAR_PATH/$JAR_NAME ./$JAR_NAME
echo -e "\033[0;32m move $TEMP_JAR_PATH/$JAR_NAME to ./$JAR_NAME success. \033[0m"
else
echo -e "\033[0;31m $TEMP_JAR_PATH or $TEMP_JAR_PATH/$JAR_NAME not exists. \033[0m"
fi
}
function restart()
{
stop
sleep 2s
start
}
function restart_p()
{
stop_p
sleep 2s
start_p
}
function bakjar()
{
date=$(date "+%Y%m%d")
time=$(date "+%H%M")
if [ ! -f "./$JAR_NAME" ]; then
echo -e "\033[0;31m $JAR_NAME not exists. \033[0m"
elif [ -f "./$JAR_NAME$date" ];then
mv $JAR_NAME $JAR_NAME$date-$time
elif [ -f "./$JAR_NAME$date-$time" ];then
mv $JAR_NAME $JAR_NAME$date-$time$(date "+%S")
else
mv $JAR_NAME $JAR_NAME$date
fi
ls
echo -e "\033[0;32m $JAR_NAME has been backuped. \033[0m"
}
function bakrestart()
{
bakjar
sleep 3s
mvtempjar
restart
}
function bakrestart_p()
{
bakjar
sleep 3s
mvtempjar
restart_p
}
# -e filename 如果 filename存在,则为真
# -d filename 如果 filename为目录,则为真
# -f filename 如果 filename为常规文件,则为真
# -L filename 如果 filename为符号链接,则为真
# -r filename 如果 filename可读,则为真
# -w filename 如果 filename可写,则为真
# -x filename 如果 filename可执行,则为真
# -s filename 如果文件长度不为0,则为真
# -h filename 如果文件是软链接,则为真
#echo -e “\033[30m 黑色字 \033[0m"
#echo -e “\033[31m 红色字 \033[0m"
#echo -e “\033[32m 绿色字 \033[0m"
#echo -e “\033[33m 黄色字 \033[0m"
#echo -e “\033[34m 蓝色字 \033[0m"
#echo -e “\033[35m 紫色字 \033[0m"
#echo -e “\033[36m 天蓝字 \033[0m"
#echo -e “\033[37m 白色字 \033[0m"
#echo -e “\033[40;37m 黑底白字 \033[0m”
#echo -e “\033[41;37m 红底白字 \033[0m"
#echo -e “\033[42;37m 绿底白字 \033[0m”
#echo -e “\033[43;37m 黄底白字 \033[0m”
#echo -e “\033[44;37m 蓝底白字 \033[0m”
#echo -e “\033[45;37m 紫底白字 \033[0m”
#echo -e “\033[46;37m 天蓝底白字 \033[0m”
#echo -e “\033[47;30m 白底黑字 \033[0m”
# 根据输入参数执行对应方法
case "$1" in
"bakrestart")
bakrestart
;;
"bakrestart-p")
bakrestart_p
;;
"restart")
restart
;;
"restart-p")
restart_p
;;
"start")
start
;;
"start-p")
start_p
;;
"stop")
stop
;;
"stop-p")
stop
;;
"status")
status
;;
"status-p")
status_p
;;
"mvtempjar")
mvtempjar
;;
*)
echo -e "\033[0;31m Usage: \033[0m \033[0;32m sh $0 {bakrestart|restart|start|stop|status|mvtempjar|bakrestart-p|restart-p|start-p|stop-p|staus-p} \033[0m\033[0;31m Example: \033[0m
\033[0;33m sh $0 start xwksh-test.jar or sh $0 start-p xwksh-test.jar 8080 \033[0m"
esac
linux操作jar包脚本
于 2022-12-02 19:14:59 首次发布