linux环境执行jar脚本

一、前言

平常工作中,我们的开发的项目部署到linux环境,以jar包的方式运行,涉及jar包的启动、停止、查看状态等,我们可以通过脚本的方式进行维护,减少自己敲打一长串的命令少敲一个字母或者多敲一个字母,方便维护。

二、脚本

vim  app.sh

#!/bin/bash

appName=xxxx.jar
if [ -z $appName ]
then
    echo "Please check that this script and your jar-package is in the same directory!"
    exit 1
fi

killForceFlag=$2

function start()
{
    count=`ps -ef |grep java|grep $appName|wc -l`
    if [ $count != 0 ];then
        echo "Maybe $appName is running, please check it..."
    else
        echo "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 ]
    then
        echo "Maybe $appName not running, please check it..."
    else
        echo -n "The $appName is stopping..."
        if [ "$killForceFlag" == "-f" ]
        then
            echo "by force"
            kill -9 $appId
        else
            echo
            kill $appId
        fi
    fi
}

function status()
{
    appId=`ps -ef |grep java|grep $appName|awk '{print $2}'`
    if [ -z $appId ]
    then
        echo -e "\033[31m Not running \033[0m"
    else
        echo -e "\033[32m Running [$appId] \033[0m"
    fi
}

function restart()
{
    stop
    for i in {3..1}
    do
        echo -n "$i "
        sleep 1
    done
    echo 0
    start
}

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 in
    start)
    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

你可能感兴趣的:(shell脚本,java,ssh)