Shell脚本:jar包启停

在日常工作中经常会启停服务,故整理成脚本,方便操作。

shell脚本内容如下:(exe.sh)

#!/bin/bash

package_name=flink-0.0.1-SNAPSHOT.jar
#current_path=/data

#入参检查
if [ $# -ne 1 ];
then
    echo -e "-->Error! Please enter the param, such as \033[33m [start/status/stop/restart] or [1/2/3/4] \033[0m"
    echo -e "-->Example : ./exe.sh start"
    exit 1
fi


function Start(){
    nohup java -jar $package_name  > nohup.log 2>&1 &
    sleep 2
    echo "进程状态:"
    Status
}

function Status(){
    echo "进程状态:"
    ps -ef | grep $package_name | grep -v grep
}

function Stop(){
    pid=`ps -ef | grep $package_name | grep -v grep| awk {'print $2'}`
    kill -9 $pid
}

function Restart(){
    Stop
    Start
}

if [ $1 == "start" ] || [ $1 == "1" ];
then    
    Start
    
elif [ $1 == "status" ] || [ $1 == "2" ];
then
    Status

elif [ $1 == "stop" ] || [ $1 == "3" ];
then
    Stop

elif [ $1 == "restart" ] || [ $1 == "4" ];
then
    Restart
else
    echo -e "--> \033[33m The param you enter is wrong! Please retry! \033[0m" $1
    echo -e "--> \033[33m The param is [start/status/stop/restart] or [1/2/3/4] \033[0m"
fi

 

使用方法:

./exe.sh [start/status/stop/restart]

或者

./exe.sh [1/2/3/4]

 

posted @ 2020-06-23 15:16  星瑞  阅读(358)  评论(0编辑  收藏  举报