linux系统应用程序启动脚本

1.run.sh

bash 复制代码
#!/bin/bash
version="1.0.0";

appName=$2
if [ -z $appName ];then
    appName=`ls -t |grep .jar$ |head -n1`
fi

function start()
{
        count=`ps -ef |grep java|grep $appName|wc -l`
        if [ $count != 0 ];then
                echo "Maybe $appName is running, please check it..."
        else
                echo "The $appName is starting..."
                nohup java -Dloader.path=./lib  -jar ./$appName  -XX:+UseG1GC -XX:+HeapDumpOnOutOfMemoryError -Xms1G -Xmx4G > /dev/null 2>&1 &
        fi
}

function stop()
{
#       appId=`ps -ef |grep java|grep $appName|awk '{print $2}'`
        appId=$(cat ./logs/abk.pid)

        if [ -z $appId ];then
            echo "Maybe $appName not running, please check it..."
        else
        echo "The $appName is stopping..."
        kill $appId
        rm -rf ./logs/abk.pid

        fi
}

function restart()
{
    # get release version
    releaseApp=`ls -t |grep .jar$ |head -n1`

    # get last version
    lastVersionApp=`ls -t |grep .jar$ |head -n2 |tail -n1`

    appName=$lastVersionApp
    stop
    for i in {5..1}
    do
        echo -n "$i "
        sleep 1
    done
    echo 0

    backup

    appName=$releaseApp
    start
}

function backup()
{
    # get backup version
    backupApp=`ls |grep -wv $releaseApp$ |grep .jar$`

    # create backup dir
    if [ ! -d "backup" ];then
        mkdir backup
    fi

    # backup
    for i in ${backupApp[@]}
    do
        echo "backup" $i
        mv $i backup
    done
}

function status()
{
    appId=`ps -ef |grep java|grep $appName|awk '{print $2}'`
        if [ -z $appId ]
        then
            echo -e "\033[31m Not running \033[0m"
       fi
}
function usage()
{
    echo "Usage: $0 {start|stop|restart|status|stop -f}"
    echo "Example: $0 start"
    exit 1
}

case $1 in
        start)
        start;;

        stop)
        stop;;

        restart)
        restart;;

        status)
        status;;

        *)
        usage;;
esac

2.start.sh

bash 复制代码
#!/bin/bash
## Author ly
version="1.0.0";
./run.sh start

3.其他

其他的stop.sh、restart.sh写法同start.sh

相关推荐
SEO_juper21 分钟前
Node.js 重定向链检测工具:301 跳了几跳、有没有成环,一次看清
xml·开发语言·python·node.js·json·bash
水饺编程1 小时前
第2章:Linux 基础,exit 命令
linux·c语言·汇编
木子欢儿1 小时前
Debian 13 + vsftpd 搭建FTP服务
linux·服务器·前端·网络·debian
GeW2 小时前
新质生产力落地实践:企业级Linux迁移与数据库国产化
linux
雯宝2 小时前
usb_serial_generic_register
linux
疯狂的Meng3 小时前
我把第一周的 Linux 命令整理成了自己的手册(附 7 个我踩的坑)
linux
wt95273 小时前
VMware 虚拟机静态 IP 配置
linux
小羊没烦恼!3 小时前
分布式事务 | 使用DTM 的Saga 模式
java·linux·运维·数据库·wpf
靳某某。4 小时前
Ubuntu安装php8.4 及redis ngin
linux·redis·ubuntu
Shell运维手记4 小时前
Shell 结构化命令知识点总结(case、for、while)
运维·microsoft·shell·脚本