一、前言
平常工作中,我们的开发的项目部署到linux环境,以jar包的方式运行,涉及jar包的启动、停止、查看状态等,我们可以通过脚本的方式进行维护,减少自己敲打一长串的命令少敲一个字母或者多敲一个字母,方便维护。
二、脚本
vim app.sh
#!/bin/bashappName=xxxx.jar
if [ -z $appName ]
thenecho "Please check that this script and your jar-package is in the same directory!"exit 1
fikillForceFlag=$2function start()
{count=`ps -ef |grep java|grep $appName|wc -l`if [ $count != 0 ];thenecho "Maybe $appName is running, please check it..."elseecho "The $appName is starting..."nohup java -jar $appName --spring.profiles.active=test >log.out 2>&1 &fi
}function stop()
{appId=`ps -ef |grep java|grep $appName|awk '{print $2}'`if [ -z $appId ]thenecho "Maybe $appName not running, please check it..."elseecho -n "The $appName is stopping..."if [ "$killForceFlag" == "-f" ]thenecho "by force"kill -9 $appIdelseechokill $appIdfifi
}function status()
{appId=`ps -ef |grep java|grep $appName|awk '{print $2}'`if [ -z $appId ]thenecho -e "\033[31m Not running \033[0m"elseecho -e "\033[32m Running [$appId] \033[0m"fi
}function restart()
{stopfor i in {3..1}doecho -n "$i "sleep 1doneecho 0start
}function help()
{echo "Usage: $0 {start|stop|restart|status|stop -f}"echo "Example: $0 start"exit 1
}function taillog()
{tail -f -n 1000 log.out
}case $1 instart)start;;stop)stop;;restart)restart;;status)status;;taillog)taillog;;*)help;;
esac
三、授权及命令
chmod +x app.sh
启动:app.sh start
停止:app.sh stop -f
查看状态 app.sh status