说句实话,我写脚本是为了懒,当时闲的没事用 springcloud 搭建了个项目,没成想最终需要启动四五个服务,在本地的话就需要切换到每个服务的主函数上面启动,结果想了个招,写个脚本就可以很方便的启动项目了,当然也是第一次写
#!/bin/bash
SCRIPT=$0
PORT=$1
OPERATOR=$2
usage() {
echo "Usage: sh $SCRIPT [app_name] [start|stop|restart|status]"
exit 1
}
if [ $# != 2 ]; then
usage
fi
if [ $PORT -eq "8761" ]
then
APP_NAME="flowers_eureka_server"
fi
getpid(){
pid=$(lsof -n -i4TCP:"${PORT}" | grep LISTEN | grep -v grep | awk '{print $2}');
if [ -n "$pid" ];
then
return 1
else
return 0
fi
}
start() {
getpid
if [ $? -eq "1" ]
then
echo "${APP_NAME} is already running. port=${PORT}."
else
cd "/Applications/workspace/intellijidea_workspace/other/flowers/"${APP_NAME}
mvn spring-boot:run
fi
}
stop(){
getpid
if [ $? -eq "1" ]
then
kill -9 $pid
else
echo "${APP_NAME} is not running"
fi
}
status(){
getpid
if [ $? -eq "1" ]; then
echo "${APP_NAME} is running. Pid is ${pid}"
else
echo "${APP_NAME} is NOT running."
fi
}
restart(){
stop
start
}
case "$OPERATOR" in
"start")
start ;;
"stop")
stop ;;
"status")
status ;;
"restart")
restart ;;
*)
usage ;;
esac
打开命令行窗口,输入
./start.sh 8761 start
既可启动项目
博主扣扣: 博主微信: