shell执行jar包相关操作指南
第一次完整的写shell脚本,之前都是写一些简单的操作。
关于微服务jar包的启动,停止,重启,全部启动,全部停止,全部重启以及修改jar包内文件后启动的相关脚本
稍微记录下:
cd /opt/micro-service/
#输入要进行的操作,启动jar
echo "you need todo?(start/stop/status/restart/startAll/stopAll/restartAll/upstart)"
read todo
#检查程序是否在运行
is_exist(){
pid=`ps -ef|grep $jarName|grep -v grep|awk '{print $2}'`
#如果不存在返回1,存在返回0
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}
#修改启动
upstart(){
#输入要操作的jar包
echo "Please Input jar Name:(servcie/servcie.jar)"
read jarName
is_exist
if [ $? -eq 0 ]; then
echo "${jarName} is already running. please stop"
else
jar tvf $jarName |grep $fileName
#选择正确的文件路径进行修改
echo "Query out as follows,Please select the copy you want"
read oldFilePath
#解压jar内文件
jar xvf $jarName $oldFilePath
sleep 1
#输入jar外替换文件路径
echo "Please Input replace file path:(like:config/config.properties)"
read newFilePath
#替换文件
cp $newFilePath $oldFilePath
echo "Replace success!"
#压缩回jar内
jar uvf $jarName $oldFilePath
sleep 2
#启动
java -jar $jarName &
fi
}
#启动
start(){
#输入要操作的jar包
echo "Please Input jar Name:(servcie/servcie.jar)"
read jarName
is_exist
if [ $? -eq 0 ]; then
echo "${jarName} is already running. pid=${pid}"
else
java -jar $jarName &
fi
}
#停止
stop(){
#输入要操作的jar包
echo "Please Input jar Name:(servcie/servcie.jar)"
read jarName
is_exist
if [ $? -eq "0" ]; then
kill -9 $pid
ps -ef|grep java
else
echo "${jarName} is not running"
fi
}
#状态
status(){
#输入要操作的jar包
echo "Please Input jar Name:(servcie/servcie.jar)"
read jarName
is_exist
if [ $? -eq "0" ]; then
echo "${jarName} is running. Pid is ${pid}"
else
echo "${jarName} is NOT running."
fi
}
#重启
restart(){
#输入要操作的jar包
echo "Please Input jar Name:(servcie/servcie.jar)"
read jarName
stop
sleep 5
start
}
#全部启动
startAll(){
for FILENAME in $(ls *jar);
do
pkill -9 $FILENAME
done
}
#全部停止
stopAll(){
for FILENAME in $(ls *jar);
do
pid=`ps -ef|grep $FILENAME|grep -v grep|awk '{print $2}'`
java -jar $pid & ;
done
}
#全部重启
restartAll(){
stopAll
sleep 10
startAll
}
case $todo in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
restart
;;
startAll)
startAll
;;
stopAll)
stopAll
;;
restartAll)
restartAll
;;
esac