使用自动化脚本杀死通过 maven 命令启动的 java 应用程序:

使用自动化脚本杀死通过 maven 命令启动的 java 应用程序:

环境规格:

  • 操作系统:Ubuntu 20.04
  • Java 版本:1.8
  • Maven 版本:3.6

我正在构建一个脚本,该脚本将使用 maven 命令启动和停止所有 java 服务。我需要使用 maven 命令来执行此操作,因为我想获取应用程序的最新版本。

我有服务A、服务B 和服务C。

我已经创建了一个启动服务的脚本。但我卡在了停止由 maven 命令启动的服务的部分。下面是脚本。

#!/usr/bin/env bash

ACTION="$1"

directories=('serviceA' 
             'serviceB'
             'serviceC'
)

commands=('mvn compile exec:java -Dexec.mainClass="pakcage.ServiceAApp"' 
          'mvn compile exec:java -Dexec.mainClass="package.ServiceBApp"'
          'mvn compile exec:java -Dexec.mainClass="pakcage.ServiceCApp"')

if [[ $ACTION == "start" ]] 
then
    #Starting all the service logic
    printf "Starting ... \n"
    
    for ((i=0; i<${#directories[@]}; ++i))
    do

        cd  ${directories[$i]} 
        eval  ${commands[$i]}
    done
    
    printf "\nFinished the operation of starting the core services. \n"
    exit 0
    #end of starting all the service
elif [[ $ACTION == "stop" ]] 
then
    #stopping all the service logic
    printf "Stopping ..."
    # WHAT TO DO HERE
    printf "\nFinished the operation of stopping the core services. \n"
    exit 0
    #end of stopping all the services
else
    printf "\nPass the argument 'start' for starting all the core services or the 'stop' argument for stopping all the core services \n"
    exit 1
fi

答案1

我通过使用命令解决了这个问题nohup,该命令即使在退出终端后仍保持程序运行。下面是命令:

service='ServiceA'
command='mvn compile exec:java -Dexec.mainClass="pakcage.ServiceAApp"'
echo service | | xargs -L 1 -I {}  sh -c "cd '{}'; echo 'Changed directory to' '{}'; nohup ${commands} > $HOME/logs/${service}.log 2>&1 & echo  \$! > $HOME/pids/${service}.pid; "

我将日志重定向到.log文件中,并将启动的进程的 pid 保存在.pid文件中。

如果我想关闭所有启动的进程,我会读取所有.pid文件并终止它们,如下所示:

s $HOME/pids | xargs -L 1 -I {}  sh -c " pkill -F $HOME/pids/'{}' ; echo 'Stopping service:'  {} | sed 's/\.[^.]*$//'; echo ''; "

以下是整个脚本:

#!/usr/bin/env bash

ACTION="$1"

directories=('serviceA' 
             'serviceB'
             'serviceC'
)

filenames=('serviceA' 
             'serviceB'
             'serviceC'
)
commands=('mvn compile exec:java -Dexec.mainClass="pakcage.ServiceAApp"' 
          'mvn compile exec:java -Dexec.mainClass="package.ServiceBApp"'
          'mvn compile exec:java -Dexec.mainClass="pakcage.ServiceCApp"')

if [[ -d $HOME/logs ]]
then
  #Directory Exists
  echo "Directory logs exits"
else
 mkdir $HOME/logs
 echo "Directory logs created in $HOME"
fi


if [[ -d $HOME/pids ]]
then
  #Directory Exists
  echo "Directory pids exits"
else
 mkdir $HOME/pids
 echo "Directory pids created in $HOME"
fi

if [[ $ACTION == "start" ]] 
then
    #Starting all the service logic
    printf "Starting ... \n"
    
    for ((i=0; i<${#directories[@]}; ++i))
    do
        
        printf "\n"
        echo "${directories[$i]}" | xargs -L 1 -I {}  sh -c "cd '{}'; echo 'Changed directory to' '{}'; nohup ${commands[$i]} > $HOME/logs/${filenames[$i]}.log 2>&1 & echo  \$! > $HOME/pids/${filenames[$i]}.pid; "
        printf "Starting ${filenames[$i]} service. Check logs for more information in path: $HOME/logs/${filenames[$i]}.log \n"
        sleep 5s
    done
    
    printf "\nFinished the operation of starting the core services. \n"
    exit 0
    #end of starting all the service
elif [[ $ACTION == "stop" ]] 
then
    #stopping all the service logic
    printf "Stopping ..."
   ls $HOME/pids | xargs -L 1 -I {}  sh -c " pkill -F $HOME/pids/'{}' ; echo 'Stopping service:'  {} | sed 's/\.[^.]*$//'; echo ''; "
    printf "\nFinished the operation of stopping the core services. \n"
    exit 0
    #end of stopping all the services
else
    printf "\nPass the argument 'start' for starting all the core services or the 'stop' argument for stopping all the core services \n"
    exit 1
fi

相关内容