linux shell脚本启动jar指定环境和端口,并打印日志到屏幕

nohup 日志重新定向到 null/dev目录中,避免时间长了,日志文件过大,占用服务器资源。
tail -f myLogs/web_info.log 追踪的日志文件是项目自己生成的。
sleep 2s休眠两秒执行追踪日志文件命令,是为了让避免 tail: no files remaining
mkdir_logfile()创建目录,和文件(记录一下,和启动jar没关系);
错误。

usage() {
    echo "Usage: sh 执行脚本.sh [start|stop|restart|status]"
    exit 1
}
 
is_exist(){
    pid=$(ps -ef | grep $APP_NAME  | grep -v grep | awk '{print $2}')
    if [ -z $pid ]; then
        return "1"
    else
        echo "${APP_NAME} is running. Pid is ${pid}"
        return "0"
    fi
}
mkdir_logfile(){
     # 判断目录是不是已经存在,如果不存在则创建,存在则输出“dir exist” 
          echo "the dir name is myLogs"
          if [ ! -d myLogs  ];then
                mkdir myLogs
                echo "success"
                cd myLogs
                touch web_info.log
                echo "success1"
          else
          echo "error"
          fi
}
start(){
    #mkdir_logfile
    is_exist
    if [ $? -eq "0" ]; then
        echo "${APP_NAME} is already running. pid=${pid} ."
    else
        echo "${app_name} is start running"
        nohup java -jar  -Xms128M -Xmx128M  $APP_NAME --server.port=$PROT --spring.profiles.active=$PROFILE  > /dev/null 2>&1 & 
        sleep 2s 
        tail -f myLogs/web_info.log
    fi
}
 
stop(){
    is_exist
    if [ $? -eq "0" ]; then
        kill -9 $pid
    else
        echo "${APP_NAME} is not running"
    fi  
}
 
status(){
    is_exist
    if [ $? -eq "0" ]; then
        echo "${APP_NAME} is running. Pid is ${pid}"
    else
        echo "${APP_NAME} is NOT running."
    fi
}
 
restart(){
    stop
    start
}
 
APP_NAME=jar名称
 
if [ -z "${APP_NAME}" ]; then
        echo "require jar name"
        exit 1
fi
PROFILE=环境
 
if [ -z "${PROFILE}" ]; then
        echo "require profile"
        exit 1
fi
PROT=端口号

if [ -z "${PROT}" ]; then
        echo "require prot"
        exit 1
fi
pid=''
 
case "$1" in
    "start")
        start
        ;;
    "stop")
        stop
        ;;
    "status")
        status
        ;;
    "restart")
        restart
        ;;
    *)
        usage
        ;;
esac

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容