#帮助
okjava -h
#运行
okjava start
#查看状态
okjava status
#重启
okjava restart
#暂停
okjava stop
参数
-h: 帮助
-n: 默认1 运行的应用数量
-p: 默认 端口号支持多个 8080,8081
-a: 默认 激活的配置文件
-f: 默认*.jar 运行的JAR文件
-t: 默认3秒 重启应用等待时间
#示例:
#启动当前目录下的JAVA文件,运行2个应用
okjava start -n=2 -p=8001,8002 -a=local
#查看当前目录下的JAVA文件,运行状态
okjava status
#重启当前目录下的JAVA文件
okjava restart -n=2 -p=8001,8002 -a=local -t=10
#停止当前目录下的JAVA文件
okjava stop
cd /usr/bin/
vi okjava
复制下面命令文本
#!/bin/bash
path=`pwd $0`
type=$1
file=""
num=""
port=""
active=""
time=""
pars=$*
if [[ "$pars" == "" || "$pars" == "-h" ]] ;then
echo " 示例: sh okjava start"
echo " 第一个参数命令类型:start启动,status状态,restart重启,stop终止"
echo " -n=1 默认1 运行的应用数量"
echo " -p=0 默认0 端口号支持多个 8080,8081 "
echo " -a=local 默认local 激活的配置文件 "
echo " -f=*.jar 默认*.jar 运行的JAR文件 "
echo " -t=3 默认3秒 重启应用等待时间 "
echo " -d= 默认当前目录 应用所在目录"
fi
## 参数处理
for par in $pars
do
if [[ "$par" =~ "-n=" ]] ;then
num=${par//-n=/}
fi
if [[ "$par" =~ "-p=" ]] ;then
port=${par//-p=/}
fi
if [[ "$par" =~ "-f=" ]] ;then
file=${par//-f=/}
fi
if [[ "$par" =~ "-a=" ]] ;then
active=${par//-a=/}
fi
if [[ "$par" =~ "-t=" ]] ;then
time=${par//-t=/}
fi
if [[ "$par" =~ "-d=" ]] ;then
path=${par//-d=/}
fi
done
## 默认参数处理
if [[ ! "start restart stop status" =~ "$type" ]] ;then
echo "error:$type in [start,restart,stop,status]"
exit
fi
if [ "$num" == "" ] ;then
num=1
fi
if [ "$time" == "" ] ;then
time=1
fi
if [[ "$file" == "" ]] ;then
file="*.jar"
fi
cd $path
portlist=(${port//,/ })
## 运行应用参数
runnum=1
runport=0
javaname=`find $path -maxdepth 1 -name "$file"`
if [ "$javaname" == "" ] ;then
echo " 未检测到应用"
exit
fi
echo "JAVA: $javaname"
## 运行一个应用
run(){
length=${#portlist[*]}
if [[ $length -ge $runnum ]] ;then
runport=${portlist[$runnum-1]}
else
runport=""
fi
par_active="--spring.profiles.active=$active"
if [[ "$active" == "" ]] ;then
par_active=""
fi
par_port="--server.port=$runport"
if [[ "$runport" == "" ]] ;then
par_port=""
fi
nohup java -jar $javaname $par_active $par_port -Duser.language=zh -Duser.region=CN &
echo " RUN[$runnum]: PORT $runport 启动应用..."
sleep 1
}
## 运行应用处理
start(){
echo "------------------ START -------------------"
idn=$(ps -ef|grep $javaname | awk '{print $2$8}')
ids=(${idn/\n/ })
st=1
for id in "${ids[@]}"
do
if [[ "$id" =~ "java" ]] ;then
pid=${id/java/}
echo " PID[$st]: $pid 应用正在运行..."
if [[ $st > $num ]] ;then
kill -s 9 $pid
echo "PID[$st]: $pid 关闭多余的应用..."
sleep 3
fi
st=$(($st+1))
fi
done
for ((i=$st;i<=$num;i++))
do
runnum=$i
run
done
echo "------------------- END --------------------"
}
restart(){
echo "----------------- RESTAET ------------------"
idn=$(ps -ef|grep $javaname | awk '{print $2$8}')
ids=(${idn/\n/ })
st=1
for id in "${ids[@]}"
do
if [[ "$id" =~ "java" ]] ;then
pid=${id/java/}
if [[ $st > $num ]] ;then
kill -s 9 $pid
echo " PID[$st]: $pid 关闭多余的应用..."
sleep 3
else
kill -s 9 $pid
echo " PID[$st]: $pid 重启应用..."
sleep 3
runnum=$st
run
sleep $time
fi
st=$(($st+1))
fi
done
#当启动的应用小于要求应用时,启动应用
for ((i=$st;i<=$num;i++))
do
runnum=$i
run
done
echo "------------------- END --------------------"
}
stop(){
echo "------------------- STOP -------------------"
idn=$(ps -ef|grep $javaname | awk '{print $2$8}')
ids=(${idn/\n/ })
for id in "${ids[@]}"
do
if [[ "$id" =~ "java" ]] ;then
pid=${id/java/}
kill -s 9 $pid
echo " 关闭应用: => $javaname => $pid"
sleep 3
fi
done
echo "------------------- END --------------------"
}
## 运行状态
status(){
echo "------------------ STATUS -------------------"
ps -ef|grep $javaname | awk '/java/{print $0}'
echo "------------------- END --------------------"
}
if [[ "$type" == "start" ]] ;then
start
exit
fi
if [[ "$type" == "restart" ]] ;then
restart
exit
fi
if [[ "$type" == "stop" ]] ;then
stop
exit
fi
if [[ "$type" == "status" ]] ;then
status
exit
fi
echo "error:$type in [start,restart,stop,status]"
chmod +x okjava
okjava -h