侧边栏壁纸
博主头像
会飞的大象博主等级

爱运动的程序猿

  • 累计撰写 126 篇文章
  • 累计创建 158 个标签
  • 累计收到 0 条评论
标签搜索

目 录CONTENT

文章目录

常用shell脚本编写

会飞的大象
2024-03-12 / 0 评论 / 0 点赞 / 305 阅读 / 908 字

通过一个shell脚本控制8个服务启、停、重启、状态查询,外部传两个参数【服务名称】和【操作类型】

服务名称有:socket-gm|socket-socsp|http-gm|http-socsp|statusPusher|statusReceiver|syncClient|syncServer.
操作类型有:start|stop|restart|status

#!/bin/bash

if [ "$1" = "socket-gm" ];then
	NowServer=ocsp-query-1.0.jar;
	NowYml=socket-gm-query;
	NowPath=/home/caStand/slaveOcsp/query;
elif [ "$1" = "socket-socsp" ];then
	NowServer=ocsp-query-1.0.jar;
	NowYml=socket-socsp-query;
	NowPath=/home/caStand/slaveOcsp/query;
elif [ "$1" = "http-gm" ];then
	NowServer=ocsp-query-1.0.jar;
	NowYml=http-gm-query;
	NowPath=/home/caStand/slaveOcsp/query;
elif [ "$1" = "http-socsp" ];then
	NowServer=ocsp-query-1.0.jar;
	NowYml=http-socsp-query;
	NowPath=/home/caStand/slaveOcsp/query;
elif [ "$1" = "statusPusher" ];then
	NowServer=ocsp-sync-1.0.jar;
	NowYml=statusPusher;
	NowPath=/home/caStand/slaveOcsp/sync;
elif [ "$1" = "statusReceiver" ];then
	NowServer=ocsp-sync-1.0.jar;
	NowYml=statusReceiver;
	NowPath=/home/caStand/slaveOcsp/sync;
elif [ "$1" = "syncClient" ];then
	NowServer=ocsp-sync-1.0.jar;
	NowYml=syncClient;
	NowPath=/home/caStand/slaveOcsp/sync;
elif [ "$1" = "syncServer" ];then
	NowServer=ocsp-sync-1.0.jar;
	NowYml=syncServer;
	NowPath=/home/caStand/slaveOcsp/sync;
else 
   echo -e "\033[0;31m 未输入应用名 \033[0m  \033[0;34m {socket-gm|socket-socsp|http-gm|http-socsp|statusPusher|statusReceiver|syncClient|syncServer} \033[0m"
   exit 1
fi

if [ "$2" = "" ];
then
    echo -e "\033[0;31m 未输入操作名 \033[0m  \033[0;34m {start|stop|restart|status} \033[0m"
    exit 1
fi

function start()
{
    count=`ps -ef |grep java|grep $NowServer |grep $NowYml |grep -v grep|wc -l`
    if [ $count != 0 ];then
        echo "$NowServer $NowYml is running..."
    else
        echo "Start $NowServer $NowYml success..."
		cd $NowPath
        nohup java  -jar $NowServer  --spring.profiles.include=$NowYml   >  /dev/null 2>&1 &
    fi
}

function stop()
{
    echo "Stop $NowServer $NowYml"
    boot_id=`ps -ef |grep java|grep $NowServer |grep $NowYml|grep -v grep|awk '{print $2}'`
    count=`ps -ef |grep java|grep $NowServer |grep $NowYml|grep -v grep|wc -l`

    if [ $count != 0 ];then
        kill $boot_id
        count=`ps -ef |grep java|grep $NowServer |grep $NowYml|grep -v grep|wc -l`

        boot_id=`ps -ef |grep java|grep $NowServer |grep $NowYml|grep -v grep|awk '{print $2}'`
        kill -9 $boot_id
    fi
}

function restart()
{
    stop
    sleep 2
    start
}

function status()
{
    count=`ps -ef |grep java|grep $NowServer |grep $NowYml|grep -v grep|wc -l`
    if [ $count != 0 ];then
        echo "$NowServer  $NowYml is running..."
    else
        echo "$NowServer  $NowYml is not running..."
    fi
}

case $2 in
    start)
    start;;
    stop)
    stop;;
    restart)
    restart;;
    status)
    status;;
    *)

    echo -e "\033[0;31m Usage: \033[0m  \033[0;34m sh  $0  {start|stop|restart|status}  {SpringBootJarName} \033[0m
\033[0;31m Example: \033[0m
      \033[0;33m sh  $0  start esmart-test.jar \033[0m"
esac

正常控制一个springboot启动、停止、重启、状态

#!/bin/bash

SpringBoot=cipher-server-1.0.0.jar

if [ "$1" = "" ];
then
    echo -e "\033[0;31m 未输入操作名 \033[0m  \033[0;34m {start|stop|restart|status} \033[0m"
    exit 1
fi

if [ "$SpringBoot" = "" ];
then
    echo -e "\033[0;31m 未输入应用名 \033[0m"
    exit 1
fi

function start()
{
    count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`
    if [ $count != 0 ];then
        echo "$SpringBoot is running..."
    else
        echo "Start $SpringBoot success..."
	cd /home/caStand/cipher
        export LD_LIBRARY_PATH=./
        nohup java  -DDUBBO_IP_TO_REGISTRY=192.168.0.210  -jar -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5015 cipher-server-1.0.0.jar   >  /dev/null 2>&1 &
    fi
}

function stop()
{
    echo "Stop $SpringBoot"
    boot_id=`ps -ef |grep java|grep $SpringBoot|grep -v grep|awk '{print $2}'`
    count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`

    if [ $count != 0 ];then
        kill $boot_id
        count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`

        boot_id=`ps -ef |grep java|grep $SpringBoot|grep -v grep|awk '{print $2}'`
        kill -9 $boot_id
    fi
}

function restart()
{
    stop
    sleep 2
    start
}

function status()
{
    count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`
    if [ $count != 0 ];then
        echo "$SpringBoot is running..."
    else
        echo "$SpringBoot is not running..."
    fi
}

case $1 in
    start)
    start;;
    stop)
    stop;;
    restart)
    restart;;
    status)
    status;;
    *)

    echo -e "\033[0;31m Usage: \033[0m  \033[0;34m sh  $0  {start|stop|restart|status}  {SpringBootJarName} \033[0m
\033[0;31m Example: \033[0m
      \033[0;33m sh  $0  start esmart-test.jar \033[0m"
esac
0

评论区