1.window 版
@echo off
set port=18092
for /f "tokens=1-5" %%i in ('netstat -ano^|findstr ":%port%"') do (
echo kill the process %%m who use the port
taskkill /pid %%m -t -f
goto start
)
:start
START "Api" java -jar rt-3.0.0.jar --server.port=18092
将此代码段做成bat脚本执行即可
2.linux版
#!/bin/sh
compare()
{
file1=`echo $1|sed -e s/-/./g`
file2=`echo $2|sed -e s/-/./g`
no=1
while true; do
p1=`echo "$file1"|cut -f $no -d .`
p2=`echo "$file2"|cut -f $no -d .`
if [ "T$p1" = "T" -a "T$p2" = "T" ]; then
return 0
elif [ "T$p1" != "T$p2" ]; then
if [ "T$p1" = "Tjar" -o "T$p1" = "T" ]; then
return 255
elif [ "T$p2" = "Tjar" -o "T$p2" = "T" ]; then
return 1;
else
num1=Y
num2=Y
expr $p1 + 0 > /dev/null 2>&1
if [ $? -ne 0 ]; then
num1=N
fi
expr $p2 + 0 > /dev/null 2>&1
if [ $? -ne 0 ]; then
num2=N
fi
if [ $num1 = "Y" -a $num2 = "Y" ]; then
if [ $p1 -gt $p2 ]; then
return 1
elif [ $p1 -lt $p2 ]; then
return 255
fi
else
return 254
fi
fi
fi
no=`expr $no + 1`
done
return 0
}
jarName=Daocha-Cloud-0.0.1-SNAPSHOT
pid=`ps -ef|grep java|grep $jarName|awk '{print $2}'`
if [ "T$pid" != "T" ]; then
echo "Stop jarName: $jarName ..."
for id in $pid; do
echo "KILL process $id."
kill -9 $id
done
fi
last=""
for file in `ls -1 $jarName.jar`; do
if [ "T$last" = "T" ]; then
last=$file
else
compare $last $file
if [ $? -eq 254 ]; then
echo "Cannot compare file version: $last vs $file."
exit 1
fi
if [ $? -eq 255 ]; then
last=$file
fi
fi
done
echo "Start jarName: $last ..."
nohup java -jar $jarName.jar > $jarName.file 2>&1 &
将此代码段做成sh脚本执行即可