mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-11-05 21:02:11 +08:00
Script Refactoring
This commit is contained in:
parent
1ea4a453ac
commit
a3906a6c93
104
scripts-new/bricks.sh
Normal file
104
scripts-new/bricks.sh
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
|
||||||
|
OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/
|
||||||
|
source "${OPENIM_ROOT}/lib/util.sh"
|
||||||
|
source "${OPENIM_ROOT}/define/binaries.sh"
|
||||||
|
source "${OPENIM_ROOT}/lib/path.sh"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#停止所有的二进制对应的进程
|
||||||
|
stop_binaries{
|
||||||
|
for binary in "${!binaries[@]}"; do
|
||||||
|
full_path=$(get_bin_full_path "$binary")
|
||||||
|
openim::util::kill_exist_binary "$full_path"
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#启动所有的二进制
|
||||||
|
start_binaries() {
|
||||||
|
local project_dir="$OPENIM_ROOT" # You should adjust this path as necessary
|
||||||
|
# Iterate over binaries defined in binary_path.sh
|
||||||
|
for binary in "${!binaries[@]}"; do
|
||||||
|
local count=${binaries[$binary]}
|
||||||
|
local bin_full_path=$(get_bin_full_path "$binary")
|
||||||
|
# Loop to start binary the specified number of times
|
||||||
|
for ((i=0; i<count; i++)); do
|
||||||
|
echo "Starting $binary instance $i: $bin_full_path -i $i -c $OPENIM_OUTPUT_CONFIG"
|
||||||
|
nohup "$bin_full_path" -i "$i" -c "$OPENIM_OUTPUT_CONFIG" > "test.log" 2>&1 &
|
||||||
|
|
||||||
|
done
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#kill二进制全路径对应的进程
|
||||||
|
kill_exist_binaries(){
|
||||||
|
for binary in "${!binaries[@]}"; do
|
||||||
|
full_path=$(get_bin_full_path "$binary")
|
||||||
|
result=$(openim::util::kill_exist_binary "$full_path" | tail -n1)
|
||||||
|
if [ "$result" -eq 0 ]; then
|
||||||
|
else
|
||||||
|
echo "$full_path running. waiting stop"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#检查所有的二进制是否退出
|
||||||
|
check_binaries_stop() {
|
||||||
|
local running_binaries=0
|
||||||
|
|
||||||
|
for binary in "${!binaries[@]}"; do
|
||||||
|
full_path=$(get_bin_full_path "$binary")
|
||||||
|
|
||||||
|
result=$(openim::util::check_process_names_exist "$full_path")
|
||||||
|
if [ "$result" -ne 0 ]; then
|
||||||
|
echo "Process for $binary is still running."
|
||||||
|
running_binaries=$((running_binaries + 1))
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
if [ "$running_binaries" -ne 0 ]; then
|
||||||
|
echo "There are $running_binaries binaries still running. Aborting..."
|
||||||
|
return 1
|
||||||
|
else
|
||||||
|
echo "All processes have been stopped."
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#检查所有的二进制是否运行
|
||||||
|
check_binaries_running{
|
||||||
|
for binary in "${!binaries[@]}"; do
|
||||||
|
expected_count=${binaries[$binary]}
|
||||||
|
full_path=$(get_bin_full_path "$binary")
|
||||||
|
|
||||||
|
result=$(openim::util::check_process_names "$full_path" "$expected_count")
|
||||||
|
if [ "$result" -eq 0 ]; then
|
||||||
|
echo "$binary is running normally."
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
echo "$binary is not running normally, $result processes missing."
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#打印所有的二进制对应的进程所所监听的端口
|
||||||
|
print_listened_ports_by_binaries{
|
||||||
|
for binary in "${!binaries[@]}"; do
|
||||||
|
expected_count=${binaries[$binary]}
|
||||||
|
base_path=$(get_bin_full_path "$binary")
|
||||||
|
for ((i=0; i<expected_count; i++)); do
|
||||||
|
full_path="${base_path} -i ${i} -c $OPENIM_OUTPUT_CONFIG"
|
||||||
|
openim::util::print_binary_ports "$full_path"
|
||||||
|
done
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
@ -30,31 +30,14 @@ source "${OPENIM_ROOT}/define/binaries.sh"
|
|||||||
source "${OPENIM_ROOT}/lib/path.sh"
|
source "${OPENIM_ROOT}/lib/path.sh"
|
||||||
|
|
||||||
|
|
||||||
for binary in "${!binaries[@]}"; do
|
|
||||||
expected_count=${binaries[$binary]}
|
|
||||||
full_path=$(get_bin_full_path "$binary")
|
|
||||||
|
|
||||||
result=$(openim::util::check_process_names "$full_path" "$expected_count")
|
|
||||||
|
|
||||||
if [ "$result" -eq 0 ]; then
|
|
||||||
echo "Startup successful for $binary"
|
|
||||||
else
|
|
||||||
echo "Startup failed for $binary, $result processes missing."
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
for binary in "${!binaries[@]}"; do
|
|
||||||
expected_count=${binaries[$binary]}
|
|
||||||
base_path=$(get_bin_full_path "$binary")
|
|
||||||
for ((i=0; i<expected_count; i++)); do
|
|
||||||
full_path="${base_path} -i ${i} -c $OPENIM_OUTPUT_CONFIG"
|
|
||||||
check_binary_ports "$full_path"
|
|
||||||
done
|
|
||||||
done
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
result=$(check_binaries_running)
|
||||||
|
ret_val=$?
|
||||||
|
|
||||||
|
if [ $ret_val -eq 0 ]; then
|
||||||
|
echo "All binaries are running."
|
||||||
|
else
|
||||||
|
echo "$result"
|
||||||
|
fi
|
||||||
|
|
||||||
|
|||||||
@ -483,59 +483,7 @@ openim::util::check_process_names_for_stop() {
|
|||||||
# Usage:
|
# Usage:
|
||||||
# openim::util::stop_services_on_ports 8080 8081 8082
|
# openim::util::stop_services_on_ports 8080 8081 8082
|
||||||
# The function returns a status of 1 if any service couldn't be stopped.
|
# The function returns a status of 1 if any service couldn't be stopped.
|
||||||
openim::util::stop_services_on_ports() {
|
|
||||||
# An array to collect ports of processes that couldn't be stopped.
|
|
||||||
local not_stopped=()
|
|
||||||
|
|
||||||
# An array to collect information about processes that were stopped.
|
|
||||||
local stopped=()
|
|
||||||
|
|
||||||
echo "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.
|
|
||||||
while read -r line; do
|
|
||||||
local pid=$(echo $line | awk '{print $2}')
|
|
||||||
|
|
||||||
# Try to stop the service by killing its process.
|
|
||||||
if kill -15 $pid; then
|
|
||||||
stopped+=($port)
|
|
||||||
else
|
|
||||||
not_stopped+=($port)
|
|
||||||
fi
|
|
||||||
done <<< "$info"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
# Print information about ports whose processes couldn't be stopped.
|
|
||||||
if [[ ${#not_stopped[@]} -ne 0 ]]; then
|
|
||||||
echo "Ports that couldn't be stopped:"
|
|
||||||
for port in "${not_stopped[@]}"; do
|
|
||||||
openim::log::status "Failed to stop service on port $port."
|
|
||||||
done
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Print information about ports whose processes were successfully stopped.
|
|
||||||
if [[ ${#stopped[@]} -ne 0 ]]; then
|
|
||||||
for port in "${stopped[@]}"; do
|
|
||||||
echo "Successfully stopped service on port $port."
|
|
||||||
done
|
|
||||||
fi
|
|
||||||
|
|
||||||
# If any of the processes couldn't be stopped, return a status of 1.
|
|
||||||
if [[ ${#not_stopped[@]} -ne 0 ]]; then
|
|
||||||
return 1
|
|
||||||
else
|
|
||||||
openim::log::success "All specified services were stopped."
|
|
||||||
echo ""
|
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
# nc -l -p 12345
|
# nc -l -p 12345
|
||||||
# nc -l -p 123456
|
# nc -l -p 123456
|
||||||
# ps -ef | grep "nc -l"
|
# ps -ef | grep "nc -l"
|
||||||
@ -549,53 +497,7 @@ openim::util::stop_services_on_ports() {
|
|||||||
# Usage:
|
# Usage:
|
||||||
# openim::util::stop_services_with_name nginx apache
|
# openim::util::stop_services_with_name nginx apache
|
||||||
# The function returns a status of 1 if any service couldn't be stopped.
|
# The function returns a status of 1 if any service couldn't be stopped.
|
||||||
openim::util::stop_services_with_name() {
|
|
||||||
# An array to collect names of processes that couldn't be stopped.
|
|
||||||
local not_stopped=()
|
|
||||||
|
|
||||||
# An array to collect information about processes that were stopped.
|
|
||||||
local stopped=()
|
|
||||||
|
|
||||||
# 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=$(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
|
|
||||||
# Print the binary path for the PID
|
|
||||||
binary_path=$(readlink -f /proc/$pid/exe)
|
|
||||||
openim::log::colorless "stop PID $pid full path: $binary_path"
|
|
||||||
|
|
||||||
# Try to stop the service by killing its process.
|
|
||||||
if kill -15 $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
|
|
||||||
return 0
|
|
||||||
|
|
||||||
}
|
|
||||||
# sleep 333333&
|
# sleep 333333&
|
||||||
# sleep 444444&
|
# sleep 444444&
|
||||||
# ps -ef | grep "sleep"
|
# ps -ef | grep "sleep"
|
||||||
@ -2845,7 +2747,8 @@ function openim::util::find_ports_for_all_services() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
check_binary_ports() {
|
|
||||||
|
function openim::util::print_binary_ports() {
|
||||||
binary_path="$1"
|
binary_path="$1"
|
||||||
|
|
||||||
# Check if the binary is running
|
# Check if the binary is running
|
||||||
@ -2886,21 +2789,6 @@ check_binary_ports() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
kill_binary() {
|
|
||||||
binary_path="$1"
|
|
||||||
|
|
||||||
pids=$(pgrep -f "$binary_path")
|
|
||||||
|
|
||||||
if [ -z "$pids" ]; then
|
|
||||||
echo "No process found for $binary_path"
|
|
||||||
else
|
|
||||||
for pid in $pids; do
|
|
||||||
echo "Killing process $pid associated with $binary_path"
|
|
||||||
kill -9 "$pid"
|
|
||||||
done
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function openim::util::kill_exist_binary() {
|
function openim::util::kill_exist_binary() {
|
||||||
local binary_path="$1"
|
local binary_path="$1"
|
||||||
|
|||||||
@ -12,57 +12,29 @@ source "$OPENIM_SCRIPTS/define/binaries.sh"
|
|||||||
# If not, you'll need to define it to return the appropriate platform directory name.
|
# If not, you'll need to define it to return the appropriate platform directory name.
|
||||||
|
|
||||||
# Main function to start binaries
|
# Main function to start binaries
|
||||||
start_binaries() {
|
|
||||||
local project_dir="$OPENIM_ROOT" # You should adjust this path as necessary
|
|
||||||
# Iterate over binaries defined in binary_path.sh
|
|
||||||
for binary in "${!binaries[@]}"; do
|
|
||||||
local count=${binaries[$binary]}
|
|
||||||
local bin_full_path=$(get_bin_full_path "$binary")
|
|
||||||
# Loop to start binary the specified number of times
|
|
||||||
for ((i=0; i<count; i++)); do
|
|
||||||
echo "Starting $binary instance $i: $bin_full_path -i $i -c $OPENIM_OUTPUT_CONFIG"
|
|
||||||
nohup "$bin_full_path" -i "$i" -c "$OPENIM_OUTPUT_CONFIG" > "test.log" 2>&1 &
|
|
||||||
|
|
||||||
done
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
kill_exist_binaries(){
|
|
||||||
for binary in "${!binaries[@]}"; do
|
|
||||||
full_path=$(get_bin_full_path "$binary")
|
|
||||||
result=$(openim::util::kill_exist_binary "$full_path" | tail -n1)
|
|
||||||
echo "result $result"
|
|
||||||
if [ "$result" -eq 0 ]; then
|
|
||||||
echo "$full_path no exist"
|
|
||||||
else
|
|
||||||
echo "$full_path running. waiting stop"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
check_all_stop() {
|
|
||||||
for binary in "${!binaries[@]}"; do
|
|
||||||
expected_count=${binaries[$binary]}
|
|
||||||
full_path=$(get_bin_full_path "$binary")
|
|
||||||
|
|
||||||
result=$(openim::util::check_process_names_exist "$full_path")
|
|
||||||
if [ "$result" -ne 0 ]; then
|
|
||||||
echo "Process for $binary is still running. Aborting..."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
echo "All processes have been stopped."
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
kill_exist_binaries
|
kill_exist_binaries
|
||||||
|
|
||||||
check_all_stop
|
result=$(check_binaries_stop)
|
||||||
|
ret_val=$?
|
||||||
|
|
||||||
|
if [ $ret_val -eq 0 ]; then
|
||||||
|
echo "All binaries are stopped."
|
||||||
|
else
|
||||||
|
echo "$result"
|
||||||
|
echo "abort..."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
# Call the main function
|
# Call the main function
|
||||||
start_binaries
|
start_binaries
|
||||||
|
|
||||||
|
check_binaries_running
|
||||||
|
|
||||||
|
print_listened_ports_by_binaries
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -10,10 +10,5 @@ source "${OPENIM_ROOT}/lib/path.sh"
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
for binary in "${!binaries[@]}"; do
|
|
||||||
expected_count=${binaries[$binary]}
|
|
||||||
full_path=$(get_bin_full_path "$binary")
|
|
||||||
kill_binary "$full_path"
|
|
||||||
done
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user