#!/bin/bash # author xtc #APP_NAME=clic-reinsurance-0.0.1-SNAPSHOT #JAR_PATH=${pwd } #echo "jar path ~~ ${JAR_PATH}/ " #使用说明,用来提示输入参数 usage() { echo -e "\033[0;31m Usage: sh 脚本名.sh [start|stop|restart|status] \033[0m" exit 1 } #检查程序是否在运行 is_exist(){ pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' ` #如果不存在返回1,存在返回0 if [ -z "${pid}" ]; then return 1 else return 0 fi } #启动方法 start(){ is_exist if [ $? -eq "0" ]; then echo "${APP_NAME} is already running. pid=${pid} ." else nohup ${JAVA_HOME}/bin/java -jar ${APP_NAME} > ${JAR_PATH}/catalina.out 2>&1 & echo "nohup ${JAVA_HOME}/bin/java -jar ${APP_NAME} > ${JAR_PATH}/catalina.out 2>&1 &" echo -e " ${APP_NAME} start success" 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 } JAR_PATH=$(cd $(dirname $0); pwd) for file in `ls ${JAR_PATH}/*.jar` do APP_NAME=${file} echo "$APP_NAME" done if [ ! -n "$APP_NAME" ]; then echo " jar is not exists " exit fi #根据输入参数,选择执行对应方法,不输入则执行使用说明 case "$1" in "start") start ;; "stop") stop ;; "status") status ;; "restart") restart ;; *) usage ;; esac
时间: 2024-04-21 22:26:10 浏览: 231
这是一个Shell脚本,用于启动、停止、重启或查看应用程序的运行状态。脚本通过输入参数来选择执行相应的操作,如果没有输入参数,则会输出使用说明。脚本中定义了几个函数,包括:
- usage:用于提示输入参数的使用说明。
- is_exist:用于检查程序是否在运行。
- start:用于启动程序。
- stop:用于停止程序。
- status:用于输出程序的运行状态。
- restart:用于重启程序。
脚本首先获取当前目录下的所有jar文件,然后根据输入参数选择执行相应的操作。如果输入的是start,则会检查程序是否已经在运行,如果没有运行,则会启动程序。如果输入的是stop,则会停止程序。如果输入的是status,则会输出程序的运行状态。如果输入的是restart,则会先停止程序,然后再启动程序。
阅读全文