#!/bin/bash

# 整个执行目录，必须要先设置 ubuntu 账号的所有权
# sudo chown -R ubuntu:ubuntu /home/www/project/BackendSys

# 增加可执行权限
# chmod +x /home/www/project/BackendSys/target/backendsys.sh
# 启动应用程序
# /home/www/project/BackendSys/target/backendsys.sh start
# 停止应用程序
# /home/www/project/BackendSys/target/backendsys.sh stop
# 重启应用程序
# /home/www/project/BackendSys/target/backendsys.sh restart
# 查看日志
# tail -f /home/www/project/BackendSys/target/backendsys.log

APP_JAR="/home/www/project/BackendSys/target/backendsys.jar"
LOG_FILE="/home/www/project/BackendSys/target/backendsys.log"

start() {
    sudo nohup java -jar $APP_JAR > "$LOG_FILE" &
    echo "App started."
}

stop() {
    ID=`ps -ef | grep "backendsys.jar" | grep -v "grep" | awk '{print $2}'`
    echo $ID

    for id in $ID
    do
      sudo kill -9 $id
      sleep 1
      sudo echo "killed $id"
    done
}

restart() {
    stop
    start
}

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

exit 0