run.sh
#! /usr/bin/bash
usage() {
echo "Usage: sh 执行脚本.sh [start|stop|restart|status]"
exit 1
}
is_exist(){
pid=$(ps -ef | grep $APP_NAME | grep 'java -jar' | grep -v grep | awk '{print $2}')
if [ -z $pid ]; then
return "1"
else
echo "${APP_NAME} is running. Pid is ${pid}"
return "0"
fi
}
start(){
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is already running. pid=${pid} ."
else
echo "${app_name} is start running"
nohup java -jar -Xms128M -Xmx400M $APP_NAME --spring.profiles.active=$PROFILE > /dev/null 2>&1 &
fi
}
stop(){
is_exist
if [ $? -eq "0" ]; then
kill -9 $pid
else
echo "${APP_NAME} is not running"
fi
}
status(){
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is running. Pid is ${pid}"
else
echo "${APP_NAME} is NOT running."
fi
}
restart(){
stop
start
}
APP_NAME=$2
if [ -z "${APP_NAME}" ]; then
echo "require jar name"
exit 1
fi
PROFILE=$3
if [ -z "${PROFILE}" ]; then
echo "require profile"
exit 1
fi
pid=''
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac
执行
$ ./run.sh restart core-facade-server-1.0-SNAPSHOT.jar dev
运行结果: