springboot的jar在linux上sh啟動指令碼

☆♂安♀★發表於2024-08-07

java 在linux上 start、stop、restart、status等啟動命令,sh指令碼,appMgr.sh 放在reources/ops下

#!/usr/bin/sh

APP_NAME="@project.name@-@project.version@.jar"
DEPLOY_PATH=`pwd`
#JVM啟動引數 1
JVM_PARAMS="-Dfastjson.parser.safeMode=true"

command =$1
#nohup java -XX:+UseG1GC -XX:+HeapDumpOnOutOfMemoryError -Xms512M -Xmx4G -jar $appName > /dev/null 2>&1 &
function start()
{
    echo "server start proces ${DEPLOY_PATH}/$APP_NAME"
    count=`ps -ef |grep java|grep $appName|wc -l`
    if [ $count != 0 ];then
        echo "server has been ${DEPLOY_PATH}/$APP_NAME"
    else
        nohup java -Xbootclasspath:a:${DEPLOY_PATH}/config -jar ${JVM_PARAMS}  ${DEPLOY_PATH}/${APP_NAME} > /dev/null 2>&1 &
        sleep 10
        status
    fi
}

function status()
{
    $appId=`ps -ef |grep ${DEPLOY_PATH}/$appName|grep -v grep|grep -v kill|awk '{print $2}'`
    if [ -z $appId ]
    then
        echo  "server is running ,appName is $appName,pid is ${$appId}"
    else
        echo  "server is running ,appName is $appName,pid is ${$appId}"
    fi
}
function stop()
{
    tpid=`ps -ef|grep ${DEPLOY_PATH}/${APP_NAME}|grep -v grep|grep -v kill|awk '{print $2}'`
    if [[ ${tpid} ]]; then
        echo "server Stop Process... $appName"
        kill -15 ${tpid}
    else
         echo "server error: ${APP_NAME} not found"
         exit 1
    fi
    sleep 5
    tpid=`ps -ef|grep ${DEPLOY_PATH}/${APP_NAME}|grep -v grep|grep -v kill|awk '{print $2}'`
    if [[ ${tpid} ]]; then
        echo "server Kill Process! ${DEPLOY_PATH}/$appName"
        kill -9 ${tpid}
    else
        echo "server Stop Success!${DEPLOY_PATH}/$appName"
    fi
}

function forcekill()
{
    tpid=`ps -ef|grep ${DEPLOY_PATH}/${APP_NAME}|grep -v grep|grep -v kill|awk '{print $2}'`
    if [[ ${tpid} ]]; then
        echo "server Kill Process! ${DEPLOY_PATH}/$appName"
        kill -9 ${tpid}
    else
        echo "server Stop Success!${DEPLOY_PATH}/$appName"
    fi
}

restart() {
    stop
    sleep 10
    start
}


case "${command}" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status
        ;;
    forcekill)
        forcekill
        ;;
    restart)
        restart
        ;;
    *)
        echo "Usage: $0 {start|stop|status|forcekill|restart}"
        exit 1
esac

pom.xml指令碼

<build>
        <finalName>${project.name}-${project.version}</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <!-- 過濾轉換引數-->
                <filtering>true</filtering>
                <includes>
                    <include>*.sh</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>false</filtering>
                <includes>
                    <include>*.sh</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.8</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <phase>test</phase>
                    </execution>
                </executions>
                <configuration>
                    <tasks>
                        <echo>moving resources below target directory</echo>
                        <copy todir="{basedir}/target/ops">
                            <fileset dir="{basedir}/src/main/resources/ops" includes="**/*.yml" />
                            <fileset dir="{basedir}/src/main/resources/ops" includes="**/*.properties" />
                            <fileset dir="{basedir}/src/main/resources/ops" includes="**/logback-spring.xml" />
                        </copy>
                        <copy todir="{basedir}/target/ops">
                            <fileset dir="{basedir}/target/config/ops" includes="*.sh" />>
                        </copy>
                    </tasks>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.2.7.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <incldeSystemScope>true</incldeSystemScope>
                            <mainClass>com.zhianchen.sftptest.SftpSpringBootApplication</mainClass>
                        </configuration>
                    </execution>
                </executions>
            </plugin>


        </plugins>
    </build>

相關文章