第一次完整的寫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