springboot项目在linux服务器部署启动脚本shell编写

#使用方法
sh small_control.sh all start 
sh small_control.sh 8080 stop
sh small_control.sh 8081 restart
#small_control.sh代码
#!/bin/bash
current_dir=$(cd `dirname $0`; pwd)       #当前目录

#判断第二个参数
case $2 in
start)
    if [ $1 == "all" ]; then    
        #第一个参数是all 遍历所有含80目录 执行small.sh
        for dir in `ls -d */ | grep 80`
        do 
            sub_shell=${current_dir}/${dir}small.sh
            if [ -x "$sub_shell" ]; then
                $sub_shell stop >/dev/null 2>&1
                $sub_shell start
            fi
        done
    else
        #第一个参数$1是port 类似8080 去执行该端口目录下的small.sh
        ${current_dir}/$1/small.sh start
    fi
    ;;
stop)
    if[ $1 == "all" ];then
        for dir in `ls -d */ | grep 80`
        do
            sub_shell=${current_dir}/${dir}small.sh
            if [ -x "$sub_shell" ];then
                $sub_shell stop
            fi
        done
    else
        ${current_dir}/$1/small.sh stop
    ;;
restart)
    if[ $1 == "all" ];then
        for dir in `ls -d */ | grep 80`
        do
            sub_shell=${current_dir}/${dir}small.sh
            if [ -x "$sub_shell" ];then
                $sub_shell restart
            fi
        done
    else
        ${current_dir}/$1/small.sh restart
    ;;
*)
    echo "Usage: small_control.sh {port|all} {start|stop}"
    echo "example: small_control.sh all start"
    echo "example: small_control.sh 8081 stop"
        exit 1
    ;;
esac    
#smal.sh
#!/bin/bash
current_dir=$(cd `dirname $0` ; pwd) #当前目录
grand_parent_dir=$(cd ${current_dir}/..;pwd) #父目录
port_num=${current_dir##*/}  #端口 截取目录最后一个/右边内容
dest_out=${current_dir}/small${port_num}.out #输出文件

jar_file=${grand_parent_dir}/small.jar
pid_file=${current_dir}/${port_num}.pid

command="java -jar -Xms1024M -Xmx4096M -Dserver.port=${port_num} ${jar_file}"

#Functions 定义方法

#是否正在运行 传入进程号
isRunning() {
    ps -p "$1" &> /dev/null
}

start() {
    #判断pid文件是否存在 如存在且在运行则返回0
    if [[ -f "$pid_file" ]]; then
        pid=$(cat "$pid_file")
        isRunning "$pid" && { echo "Alreday running [$pid]"; return 0 }
    fi
    do_start "$@" # $@ 传递所有参数到下一个方法
}

do_start(){
    cd $current_dir
    echo $command  #打印命令
    $command > $dest_out 2>&1 & #执行命令
    pid=$!  #记录进程号
    echo "$pid" > "$pid_file" #进程号输出到pid file
    # -z 判断字符串长度是否为0 为0则true
    [[ -z $pid ]] && { echo "Failed to start"; return 1; }
    echo "Started {$pid}"
}

stop(){
    # -f 判断pid文件是否存在 
    [[ -f $pid_file ]] || { echo "Not running (Pidfile not found)"; return 0; }
    pid=$(cat "$pid_file")
    isRunning "$pid" || { echo "not running (process ${pid}). remove pid file."; rm -f "$pid_file"; return 0; }
    do_stop "$pid" "$pid_file"
}

do_stop(){
    #杀掉进程 失败返回
    kill "$1" &> /dev/null || { echo "unable to kill process $1"; return 1; }
    #循环判断是否还运行
    for i in $(seq 1 30); do
        isRunning "$1" || { echo "stopped [$1]"; rm -rf "$2"; return 0; }
        [[ $i -eq 10 ]] && kill "$1" &> /dev/null
        [[ $i -eq 20 ]] && kill -9 "$1" &> /dev/null
        sleep 1
    done
    echo "unable to kill process $1";
    return 1;
}

restart(){
    stop && start
}

#运行状态
status(){
    [[ -f "$pid_file" ]] || { echo "not running"; return 3; }
    pid=$(cat "$pid_file")
    isRunning $pid || { echo "Not running (Pidfile not found)"; return 1; }
    echo "running {$pid}"
    return 0
}

case "$1" in
    start)
     #执行start方法
        start "$@"; exit $? ;;
    stop)
        stop "$@"; exit $? ;;
    restart)
        restart "$@"; exit $? ;;
    status)
        status "$@"; exit $? ;;
    *)
        echo "usage $0 {start|stop|restart|status}"
        echo 1;;
esac
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值