Используйте автоматический скрипт для убийства приложения Java, запущены с командами Maven:

Спецификация среды:

  • ОС: Ubuntu 20.04
  • Java версия: 1.8
  • Maven версия: 3.6

Я строю сценарий, который начнется и Остановите все услуги Java, используя команду Maven. Мне нужно сделать это с помощью команд Maven, потому что я хочу получить последнюю версию приложения.

У меня есть сервис, сервис и сервис.

Я создал сценарий, который начинает службу. Но я застрял со стороны, когда остановить службу, созданную команды 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
0
задан 10 March 2021 в 14:47

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
0
ответ дан 18 March 2021 в 23:27

Другие вопросы по тегам:

Похожие вопросы: