mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-01 15:45:59 +08:00
feat: save all file
Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com>
This commit is contained in:
@@ -1,5 +1,26 @@
|
||||
# OpenIM Scripts Directory Structure
|
||||
|
||||
- [OpenIM Scripts Directory Structure](#openim-scripts-directory-structure)
|
||||
- [log directory](#log-directory)
|
||||
- [Supported platforms](#supported-platforms)
|
||||
- [Get started quickly - demo.sh](#get-started-quickly---demosh)
|
||||
- [Guide: Using and Understanding OpenIM Utility Functions](#guide-using-and-understanding-openim-utility-functions)
|
||||
- [Table of Contents](#table-of-contents)
|
||||
- [1. Checking the Status of Services by Ports](#1-checking-the-status-of-services-by-ports)
|
||||
- [Function: `openim::util::check_ports`](#function-openimutilcheck_ports)
|
||||
- [Example:](#example)
|
||||
- [2. Checking the Status of Services by Process Names](#2-checking-the-status-of-services-by-process-names)
|
||||
- [Function: `openim::util::check_process_names`](#function-openimutilcheck_process_names)
|
||||
- [Example:](#example-1)
|
||||
- [3. Stopping Services by Ports](#3-stopping-services-by-ports)
|
||||
- [Function: `openim::util::stop_services_on_ports`](#function-openimutilstop_services_on_ports)
|
||||
- [Example:](#example-2)
|
||||
- [4. Stopping Services by Process Names](#4-stopping-services-by-process-names)
|
||||
- [Function: `openim::util::stop_services_with_name`](#function-openimutilstop_services_with_name)
|
||||
- [Example:](#example-3)
|
||||
- [examples](#examples)
|
||||
|
||||
|
||||
This document outlines the directory structure for scripts in the OpenIM Server project. These scripts play a critical role in various areas like building, deploying, running and managing the services of OpenIM.
|
||||
|
||||
```bash
|
||||
@@ -126,6 +147,119 @@ asciinema rec
|
||||
svg-term --cast=<movie-id> --out _output/demo.svg --window
|
||||
```
|
||||
|
||||
Here you will learn how to test a script, We take the four functions for starting and checking a service as an example.
|
||||
|
||||
## Guide: Using and Understanding OpenIM Utility Functions
|
||||
|
||||
This document provides an overview of the four utility functions designed for managing processes and services. These functions can check the status of services based on ports and process names, as well as stop services based on the same criteria.
|
||||
|
||||
### Table of Contents
|
||||
- [1. Checking the Status of Services by Ports](#checking-the-status-of-services-by-ports)
|
||||
- [2. Checking the Status of Services by Process Names](#checking-the-status-of-services-by-process-names)
|
||||
- [3. Stopping Services by Ports](#stopping-services-by-ports)
|
||||
- [4. Stopping Services by Process Names](#stopping-services-by-process-names)
|
||||
|
||||
### 1. Checking the Status of Services by Ports
|
||||
|
||||
#### Function: `openim::util::check_ports`
|
||||
|
||||
This function checks the status of services running on specified ports.
|
||||
|
||||
**Usage**:
|
||||
|
||||
```bash
|
||||
openim::util::check_ports <port1> <port2> ...
|
||||
```
|
||||
|
||||
**Design**:
|
||||
|
||||
- The function iterates through each provided port.
|
||||
- It uses the `lsof` command to identify if there is a service running on the specified port.
|
||||
- If a service is running, it logs the command, PID, and start time of the service.
|
||||
- If a service is not running, it logs that the port is not started.
|
||||
- If any service is not running, the function returns a status of 1.
|
||||
|
||||
#### Example:
|
||||
|
||||
```bash
|
||||
openim::util::check_ports 8080 8081 8082
|
||||
```
|
||||
|
||||
### 2. Checking the Status of Services by Process Names
|
||||
|
||||
#### Function: `openim::util::check_process_names`
|
||||
|
||||
This function checks the status of services based on their process names.
|
||||
|
||||
**Usage**:
|
||||
|
||||
```bash
|
||||
openim::util::check_process_names <process_name1> <process_name2> ...
|
||||
```
|
||||
|
||||
**Design**:
|
||||
|
||||
- The function uses `pgrep` to find process IDs associated with the given process names.
|
||||
- If processes are found, it logs the command, PID, associated port, and start time.
|
||||
- If no processes are found for a name, it logs that the process is not started.
|
||||
- If any process is not running, the function returns a status of 1.
|
||||
|
||||
#### Example:
|
||||
|
||||
```bash
|
||||
openim::util::check_process_names nginx mysql redis
|
||||
```
|
||||
|
||||
### 3. Stopping Services by Ports
|
||||
|
||||
#### Function: `openim::util::stop_services_on_ports`
|
||||
|
||||
This function attempts to stop services running on the specified ports.
|
||||
|
||||
**Usage**:
|
||||
|
||||
```bash
|
||||
openim::util::stop_services_on_ports <port1> <port2> ...
|
||||
```
|
||||
|
||||
**Design**:
|
||||
|
||||
- The function uses the `lsof` command to identify services running on the specified ports.
|
||||
- If a service is running on a port, it tries to terminate the associated process using the `kill` command.
|
||||
- It logs successful terminations and any failures.
|
||||
- If any service couldn't be stopped, the function returns a status of 1.
|
||||
|
||||
#### Example:
|
||||
|
||||
```bash
|
||||
openim::util::stop_services_on_ports 8080 8081 8082
|
||||
```
|
||||
|
||||
### 4. Stopping Services by Process Names
|
||||
|
||||
#### Function: `openim::util::stop_services_with_name`
|
||||
|
||||
This function attempts to stop services based on their process names.
|
||||
|
||||
**Usage**:
|
||||
|
||||
```bash
|
||||
openim::util::stop_services_with_name <process_name1> <process_name2> ...
|
||||
```
|
||||
|
||||
**Design**:
|
||||
|
||||
- The function uses `pgrep` to identify processes associated with the specified names.
|
||||
- If processes are found, it tries to terminate them using the `kill` command.
|
||||
- It logs successful terminations and any failures.
|
||||
- If any service couldn't be stopped, the function returns a status of 1.
|
||||
|
||||
#### Example:
|
||||
|
||||
```bash
|
||||
openim::util::stop_services_with_name nginx apache
|
||||
```
|
||||
|
||||
|
||||
## examples
|
||||
Scripts to perform various build, install, analysis, etc operations.
|
||||
|
||||
@@ -137,6 +137,8 @@ def "API_OPENIM_PORT" "10002" # API的开放端口
|
||||
def "API_LISTEN_IP" "0.0.0.0" # API的监听IP
|
||||
|
||||
###################### RPC Port Configuration Variables ######################
|
||||
# For launching multiple programs, just fill in multiple ports separated by commas
|
||||
# For example, [10110, 10111]
|
||||
def "OPENIM_USER_PORT" "10110" # OpenIM用户服务端口
|
||||
def "OPENIM_FRIEND_PORT" "10120" # OpenIM朋友服务端口
|
||||
def "OPENIM_MESSAGE_PORT" "10130" # OpenIM消息服务端口
|
||||
|
||||
@@ -1,103 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Copyright © 2023 OpenIM. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
set -o errexit
|
||||
set +o nounset
|
||||
set -o pipefail
|
||||
|
||||
OPENIM_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")"/../.. && pwd -P)
|
||||
[[ -z ${COMMON_SOURCED} ]] && source ${OPENIM_ROOT}/scripts/install/common.sh
|
||||
|
||||
SERVER_NAME="openim-crontask"
|
||||
|
||||
openim::log::status "Start OpenIM Cron, binary root: ${SERVER_NAME}"
|
||||
openim::log::info "Start OpenIM Cron, path: ${OPENIM_CRONTASK_BINARY}"
|
||||
|
||||
# openim::util::stop_services_with_name ${SERVER_NAME}
|
||||
|
||||
# sleep 1
|
||||
|
||||
# openim::log::status "start cron_task process, path: ${OPENIM_CRONTASK_BINARY}"
|
||||
# nohup ${OPENIM_CRONTASK_BINARY} >>${LOG_FILE} 2>&1 &
|
||||
# openim::util::check_process_names ${SERVER_NAME}
|
||||
|
||||
# # Print the necessary information after installation
|
||||
# function openim::crontask::info() {
|
||||
# cat << EOF
|
||||
# openim-crontask listen on: ${OPENIM_CRONTASK_HOST}
|
||||
# EOF
|
||||
# }
|
||||
|
||||
# install openim-crontask
|
||||
function openim::crontask::install()
|
||||
{
|
||||
pushd ${OPENIM_ROOT}
|
||||
|
||||
# 1. Build openim-crontask
|
||||
make build BINS=${SERVER_NAME}
|
||||
openim::common::sudo "cp ${OPENIM_OUTPUT_HOSTBIN}/${SERVER_NAME} ${OPENIM_INSTALL_DIR}/bin"
|
||||
|
||||
# 2. Generate and install the openim-crontask configuration file (openim-crontask.yaml)
|
||||
echo ${LINUX_PASSWORD} | sudo -S bash -c \
|
||||
"./scripts/genconfig.sh ${ENV_FILE} deployments/templates/openim-crontask.yaml > ${OPENIM_CONFIG_DIR}/openim-crontask.yaml"
|
||||
|
||||
# 3. Create and install the openim-crontask systemd unit file
|
||||
echo ${LINUX_PASSWORD} | sudo -S bash -c \
|
||||
"./scripts/genconfig.sh ${ENV_FILE} deployments/templates/init/openim-crontask.service > /etc/systemd/system/openim-crontask.service"
|
||||
|
||||
# 4. Start the openim-crontask service
|
||||
openim::common::sudo "systemctl daemon-reload"
|
||||
openim::common::sudo "systemctl restart openim-crontask"
|
||||
openim::common::sudo "systemctl enable openim-crontask"
|
||||
openim::crontask::status || return 1
|
||||
openim::crontask::info
|
||||
|
||||
openim::log::info "install openim-crontask successfully"
|
||||
popd
|
||||
}
|
||||
|
||||
# Unload
|
||||
function openim::crontask::uninstall()
|
||||
{
|
||||
set +o errexit
|
||||
openim::common::sudo "systemctl stop openim-crontask"
|
||||
openim::common::sudo "systemctl disable openim-crontask"
|
||||
openim::common::sudo "rm -f ${OPENIM_INSTALL_DIR}/bin/openim-crontask"
|
||||
openim::common::sudo "rm -f ${OPENIM_CONFIG_DIR}/openim-crontask.yaml"
|
||||
openim::common::sudo "rm -f /etc/systemd/system/openim-crontask.service"
|
||||
set -o errexit
|
||||
openim::log::info "uninstall openim-crontask successfully"
|
||||
}
|
||||
|
||||
# Status Check
|
||||
function openim::crontask::status()
|
||||
{
|
||||
# 查看 openim-crontask 运行状态,如果输出中包含 active (running) 字样说明 openim-crontask 成功启动。
|
||||
systemctl status openim-crontask|grep -q 'active' || {
|
||||
openim::log::error "openim-crontask failed to start, maybe not installed properly"
|
||||
return 1
|
||||
}
|
||||
|
||||
# 监听端口在配置文件中是 hardcode
|
||||
if echo | telnet 127.0.0.1 7070 2>&1|grep refused &>/dev/null;then
|
||||
openim::log::error "cannot access health check port, openim-crontask maybe not startup"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
if [[ "$*" =~ openim::crontask:: ]];then
|
||||
eval $*
|
||||
fi
|
||||
@@ -13,8 +13,6 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
# Common utilities, variables and checks for all build scripts.
|
||||
|
||||
set -o errexit
|
||||
set +o nounset
|
||||
set -o pipefail
|
||||
@@ -22,9 +20,94 @@ set -o pipefail
|
||||
OPENIM_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")"/../.. && pwd -P)
|
||||
[[ -z ${COMMON_SOURCED} ]] && source ${OPENIM_ROOT}/scripts/install/common.sh
|
||||
|
||||
#Include shell font styles and some basic information
|
||||
source $SCRIPTS_ROOT/lib/init.sh
|
||||
source $SCRIPTS_ROOT/path_info.sh
|
||||
SERVER_NAME="openim-push"
|
||||
|
||||
# openim::log::status "Start OpenIM Push, binary root: ${SERVER_NAME}"
|
||||
# openim::log::info "Start OpenIM Push, path: ${OPENIM_PUSH_BINARY}"
|
||||
|
||||
# openim::util::stop_services_with_name ${SERVER_NAME}
|
||||
|
||||
# openim::log::status "start push process, path: ${OPENIM_PUSH_BINARY}"
|
||||
# nohup ${OPENIM_PUSH_BINARY} >>${LOG_FILE} 2>&1 &
|
||||
# openim::util::check_process_names ${SERVER_NAME}
|
||||
|
||||
###################################### Linux Systemd ######################################
|
||||
SYSTEM_FILE_PATH="/etc/systemd/system/${SERVER_NAME}.service"
|
||||
|
||||
# Print the necessary information after installation
|
||||
function openim::push::info() {
|
||||
cat << EOF
|
||||
openim-push listen on: ${OPENIM_PUSH_HOST}
|
||||
EOF
|
||||
}
|
||||
|
||||
# install openim-push
|
||||
function openim::push::install()
|
||||
{
|
||||
pushd ${OPENIM_ROOT}
|
||||
|
||||
# 1. Build openim-push
|
||||
make build BINS=${SERVER_NAME}
|
||||
openim::common::sudo "cp ${OPENIM_OUTPUT_HOSTBIN}/${SERVER_NAME} ${OPENIM_INSTALL_DIR}/bin"
|
||||
|
||||
openim::log::status "${SERVER_NAME} binary: ${OPENIM_INSTALL_DIR}/bin/${SERVER_NAME}"
|
||||
|
||||
# 2. Generate and install the openim-push configuration file (openim-push.yaml)
|
||||
echo ${LINUX_PASSWORD} | sudo -S bash -c \
|
||||
"./scripts/genconfig.sh ${ENV_FILE} deployments/templates/${SERVER_NAME}.yaml > ${OPENIM_CONFIG_DIR}/${SERVER_NAME}.yaml"
|
||||
openim::log::status "${SERVER_NAME} config file: ${OPENIM_CONFIG_DIR}/${SERVER_NAME}.yaml"
|
||||
|
||||
# 3. Create and install the ${SERVER_NAME} systemd unit file
|
||||
echo ${LINUX_PASSWORD} | sudo -S bash -c \
|
||||
"./scripts/genconfig.sh ${ENV_FILE} deployments/templates/init/${SERVER_NAME}.service > ${SYSTEM_FILE_PATH}"
|
||||
openim::log::status "${SERVER_NAME} systemd file: ${SYSTEM_FILE_PATH}"
|
||||
|
||||
# 4. Start the openim-push service
|
||||
openim::common::sudo "systemctl daemon-reload"
|
||||
openim::common::sudo "systemctl restart ${SERVER_NAME}"
|
||||
openim::common::sudo "systemctl enable ${SERVER_NAME}"
|
||||
openim::push::status || return 1
|
||||
openim::push::info
|
||||
|
||||
openim::log::info "install ${SERVER_NAME} successfully"
|
||||
popd
|
||||
}
|
||||
|
||||
|
||||
# Unload
|
||||
function openim::push::uninstall()
|
||||
{
|
||||
set +o errexit
|
||||
openim::common::sudo "systemctl stop ${SERVER_NAME}"
|
||||
openim::common::sudo "systemctl disable ${SERVER_NAME}"
|
||||
openim::common::sudo "rm -f ${OPENIM_INSTALL_DIR}/bin/${SERVER_NAME}"
|
||||
openim::common::sudo "rm -f ${OPENIM_CONFIG_DIR}/${SERVER_NAME}.yaml"
|
||||
openim::common::sudo "rm -f /etc/systemd/system/${SERVER_NAME}.service"
|
||||
set -o errexit
|
||||
openim::log::info "uninstall ${SERVER_NAME} successfully"
|
||||
}
|
||||
|
||||
# Status Check
|
||||
function openim::push::status()
|
||||
{
|
||||
# Check the running status of the ${SERVER_NAME}. If active (running) is displayed, the ${SERVER_NAME} is started successfully.
|
||||
systemctl status ${SERVER_NAME}|grep -q 'active' || {
|
||||
openim::log::error "${SERVER_NAME} failed to start, maybe not installed properly"
|
||||
return 1
|
||||
}
|
||||
|
||||
# The listening port is hardcode in the configuration file
|
||||
if echo | telnet 127.0.0.1 7071 2>&1|grep refused &>/dev/null;then # Assuming a different port for push
|
||||
openim::log::error "cannot access health check port, ${SERVER_NAME} maybe not startup"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
if [[ "$*" =~ ${SERVER_NAME}:: ]];then
|
||||
eval $*
|
||||
fi
|
||||
|
||||
|
||||
|
||||
cd $SCRIPTS_ROOT
|
||||
|
||||
|
||||
+73
-36
@@ -18,6 +18,10 @@
|
||||
# Usage: source scripts/lib/util.sh
|
||||
################################################################################
|
||||
|
||||
# TODO Debug: Just for testing, please comment out
|
||||
# OPENIM_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")"/../.. && pwd -P)
|
||||
# source "${OPENIM_ROOT}/scripts/lib/logging.sh"
|
||||
|
||||
#1、将IP写在一个文件里,比如文件名为hosts_file,一行一个IP地址。
|
||||
#2、修改ssh-mutual-trust.sh里面的用户名及密码,默认为root用户及密码123。
|
||||
# hosts_file_path="path/to/your/hosts/file"
|
||||
@@ -260,6 +264,7 @@ openim::util::check_ports() {
|
||||
# An array to collect information about processes that are running.
|
||||
local started=()
|
||||
|
||||
openim::log::info "Checking ports: $*"
|
||||
# Iterate over each given port.
|
||||
for port in "$@"; do
|
||||
# Use the `lsof` command to find process information related to the given port.
|
||||
@@ -303,6 +308,7 @@ openim::util::check_ports() {
|
||||
return 0
|
||||
fi
|
||||
}
|
||||
# openim::util::check_ports 9090 9092
|
||||
|
||||
# The `openim::util::check_process_names` function analyzes the state of processes based on given names.
|
||||
# It accepts multiple process names as arguments and prints:
|
||||
@@ -312,33 +318,37 @@ openim::util::check_ports() {
|
||||
# openim::util::check_process_names nginx mysql redis
|
||||
# The function returns a status of 1 if any of the processes is not running.
|
||||
openim::util::check_process_names() {
|
||||
# An array to collect names of processes that are not running.
|
||||
# Arrays to collect details of processes
|
||||
local not_started=()
|
||||
|
||||
# An array to collect information about processes that are running.
|
||||
local started=()
|
||||
|
||||
# Iterate over each given process name.
|
||||
openim::log::info "Checking processes: $*"
|
||||
# Iterate over each given process name
|
||||
for process_name in "$@"; do
|
||||
# Use the `pgrep` command to find process information related to the given process name.
|
||||
local pid=$(pgrep -f $process_name)
|
||||
# If there's no process information, it means the process with the given name is not running.
|
||||
if [[ -z $pid ]]; then
|
||||
# Use `pgrep` to find process IDs related to the given process name
|
||||
local pids=($(pgrep -f $process_name))
|
||||
|
||||
# Check if any process IDs were found
|
||||
if [[ ${#pids[@]} -eq 0 ]]; then
|
||||
not_started+=($process_name)
|
||||
else
|
||||
# If there's process information, extract relevant details:
|
||||
# Command Name, and Start Time.
|
||||
# local pid=$(echo $info | awk '{print $2}')
|
||||
local command=$(ps -p $pid -o cmd=)
|
||||
local start_time=$(ps -p $pid -o lstart=)
|
||||
started+=("Process $process_name - Command: $command, PID: $pid, Start time: $start_time")
|
||||
echo "---------------command=$command"
|
||||
echo "---------------pid=$pid"
|
||||
echo "---------------start_time=$start_time"
|
||||
# If there are PIDs, loop through each one
|
||||
for pid in "${pids[@]}"; do
|
||||
local command=$(ps -p $pid -o cmd=)
|
||||
local start_time=$(ps -p $pid -o lstart=)
|
||||
local port=$(ss -ltnp 2>/dev/null | grep $pid | awk '{print $4}' | cut -d ':' -f2)
|
||||
|
||||
# Check if port information was found for the PID
|
||||
if [[ -z $port ]]; then
|
||||
port="N/A"
|
||||
fi
|
||||
|
||||
started+=("Process $process_name - Command: $command, PID: $pid, Port: $port, Start time: $start_time")
|
||||
done
|
||||
fi
|
||||
done
|
||||
|
||||
# Print information about processes which are not running.
|
||||
# Print information
|
||||
if [[ ${#not_started[@]} -ne 0 ]]; then
|
||||
openim::log::info "Not started processes:"
|
||||
for process_name in "${not_started[@]}"; do
|
||||
@@ -346,7 +356,6 @@ openim::util::check_process_names() {
|
||||
done
|
||||
fi
|
||||
|
||||
# Print information about processes which are running.
|
||||
if [[ ${#started[@]} -ne 0 ]]; then
|
||||
echo
|
||||
openim::log::info "Started processes:"
|
||||
@@ -355,7 +364,7 @@ openim::util::check_process_names() {
|
||||
done
|
||||
fi
|
||||
|
||||
# If any of the processes is not running, return a status of 1.
|
||||
# Return status
|
||||
if [[ ${#not_started[@]} -ne 0 ]]; then
|
||||
return 1
|
||||
else
|
||||
@@ -363,6 +372,7 @@ openim::util::check_process_names() {
|
||||
return 0
|
||||
fi
|
||||
}
|
||||
# openim::util::check_process_names docker-pr
|
||||
|
||||
# The `openim::util::stop_services_on_ports` function stops services running on specified ports.
|
||||
# It accepts multiple ports as arguments and performs the following:
|
||||
@@ -378,22 +388,25 @@ openim::util::stop_services_on_ports() {
|
||||
# An array to collect information about processes that were stopped.
|
||||
local stopped=()
|
||||
|
||||
openim::log::info "Stopping services on ports: $*"
|
||||
# Iterate over each given port.
|
||||
for port in "$@"; do
|
||||
# Use the `lsof` command to find process information related to the given port.
|
||||
info=$(lsof -i :$port -n -P | grep LISTEN || true)
|
||||
|
||||
|
||||
# If there's process information, it means the process associated with the port is running.
|
||||
if [[ -n $info ]]; then
|
||||
# Extract the Process ID.
|
||||
local pid=$(echo $info | awk '{print $2}')
|
||||
|
||||
# Try to stop the service by killing its process.
|
||||
if kill -TERM $pid; then
|
||||
stopped+=($port)
|
||||
else
|
||||
not_stopped+=($port)
|
||||
fi
|
||||
while read -r line; do
|
||||
local pid=$(echo $line | awk '{print $2}')
|
||||
|
||||
# Try to stop the service by killing its process.
|
||||
if kill -TERM $pid; then
|
||||
stopped+=($port)
|
||||
else
|
||||
not_stopped+=($port)
|
||||
fi
|
||||
done <<< "$info"
|
||||
fi
|
||||
done
|
||||
|
||||
@@ -422,6 +435,10 @@ openim::util::stop_services_on_ports() {
|
||||
return 0
|
||||
fi
|
||||
}
|
||||
# nc -l -p 12345
|
||||
# nc -l -p 123456
|
||||
# ps -ef | grep "nc -l"
|
||||
# openim::util::stop_services_on_ports 1234 12345
|
||||
|
||||
|
||||
# The `openim::util::stop_services_with_name` function stops services with specified names.
|
||||
@@ -438,22 +455,39 @@ openim::util::stop_services_with_name() {
|
||||
# An array to collect information about processes that were stopped.
|
||||
local stopped=()
|
||||
|
||||
openim::log::info "Stopping services with names: $*"
|
||||
# Iterate over each given service name.
|
||||
for server_name in "$@"; do
|
||||
# Use the `pgrep` command to find process IDs related to the given service name.
|
||||
local pids=$(ps aux | awk -v pattern="$server_name" '$0 ~ pattern {print $2}')
|
||||
local pids=$(pgrep -f "$server_name")
|
||||
|
||||
# If no process was found with the name, add it to the not_stopped list
|
||||
if [[ -z $pids ]]; then
|
||||
not_stopped+=("$server_name")
|
||||
continue
|
||||
fi
|
||||
local stopped_this_time=false
|
||||
for pid in $pids; do
|
||||
|
||||
# Exclude the PID of the current script
|
||||
if [[ "$pid" == "$$" ]]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
# If there's a Process ID, it means the service with the name is running.
|
||||
if [[ -n $pid ]]; then
|
||||
# Try to stop the service by killing its process.
|
||||
if kill -TERM $pid; then
|
||||
stopped+=($server_name)
|
||||
else
|
||||
not_stopped+=($server_name)
|
||||
if kill -TERM $pid 2>/dev/null; then
|
||||
stopped_this_time=true
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
if $stopped_this_time; then
|
||||
stopped+=("$server_name")
|
||||
else
|
||||
not_stopped+=("$server_name")
|
||||
fi
|
||||
done
|
||||
|
||||
# Print information about services whose processes couldn't be stopped.
|
||||
@@ -472,9 +506,13 @@ openim::util::stop_services_with_name() {
|
||||
openim::log::info "Successfully stopped the $name service."
|
||||
done
|
||||
fi
|
||||
|
||||
openim::log::success "All specified services were stopped."
|
||||
}
|
||||
|
||||
# sleep 333333&
|
||||
# sleep 444444&
|
||||
# ps -ef | grep "sleep"
|
||||
# openim::util::stop_services_with_name "sleep 333333" "sleep 444444"
|
||||
|
||||
# This figures out the host platform without relying on golang. We need this as
|
||||
# we don't want a golang install to be a prerequisite to building yet we need
|
||||
@@ -1150,7 +1188,6 @@ function openim::util::get_server_ip() {
|
||||
echo "$IP"
|
||||
}
|
||||
|
||||
|
||||
function openim::util::onCtrlC () {
|
||||
#Capture CTRL+C, terminate the background process of the program when the script is terminated in the form of ctrl+c
|
||||
kill -9 ${do_sth_pid} ${progress_pid}
|
||||
|
||||
Reference in New Issue
Block a user