一、创建脚本文件
   
1、脚本文件:app.sh
    #!/bin/bash
    
    #建议使用 . xx.sh 命令执行脚本。如果使用sh xx.sh执行,注意is_exist方法里的注释
    
    #获取进程名称,必须为完整程序名,否则可能会误操作其他进程
    
    APP_NAME=app.jar
    
    usage(){
    
    
    echo “Usage: sh app.sh [start|stop|restart|status]”
    
    exit 1
    
    }
    
    is_exist(){
    
    
    #过滤grep命令本身
    
    #注意②
    
    pid=`ps -ef|grep $APP_NAME|grep -v grep|awk ‘{print $2}’ `
    
    #使用sh xx.sh命令执行的话,启用下面代码
    
    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 -jar $APP_NAME > nohup.log 2>&1 &
    
    echo “${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
    
    }
    
    case “$1” in
    
    “start”)
    
    start ;;
    
    “stop”)
    
    stop ;;
    
    “status”)
    
    status ;;
    
    “restart”)
    
    restart ;;
    
    *)
    
    usage ;;
    
    esac
   
2、执行脚本文件
启动:sh app.sh start
重启:sh app.sh restart
停止:sh app.sh stop
3、查看运行日志
tail -f nohup.log
 
