diff --git a/scripts/check-all-by-signal.sh b/scripts/check-all-by-signal.sh index 4ea73b780..f953c08e2 100644 --- a/scripts/check-all-by-signal.sh +++ b/scripts/check-all-by-signal.sh @@ -60,6 +60,8 @@ else openim::util::check_ports ${OPENIM_DEPENDENCY_PORT_LISTARIES[@]} || return 0 fi + + if [[ $? -ne 0 ]]; then openim::log::error_exit "The service does not start properly, please check the port, query variable definition!" echo "+++ https://github.com/openimsdk/open-im-server/tree/main/scripts/install/environment.sh +++" @@ -72,12 +74,12 @@ openim::log::info "\n## Check OpenIM service name" openim::log::info "\n## Check all OpenIM service ports" echo "+++ The port being checked: ${OPENIM_SERVER_PORT_LISTARIES[@]}" -openim::util::check_ports ${OPENIM_SERVER_PORT_LISTARIES[@]} -if [[ $? -ne 0 ]]; then +openim::util::check_ports_by_signal ${OPENIM_SERVER_PORT_LISTARIES[@]} +if [[ $? -eq 0 ]]; then + openim::log::error_exit "The service does not stop properly, there are still processes running, please check the port, query variable definition!" +else echo "+++ cat openim log file >>> ${LOG_FILE}" echo "++++ All openim service ports stop successfully !" -else - openim::log::error_exit "The service does not stop properly, please check the port, query variable definition!" fi set -e diff --git a/scripts/lib/util.sh b/scripts/lib/util.sh index 8d97d6d9b..7f1c9e6ac 100755 --- a/scripts/lib/util.sh +++ b/scripts/lib/util.sh @@ -370,6 +370,88 @@ openim::util::check_ports() { fi } + +openim::util::check_ports_by_signal() { + # An array to collect ports of processes that are not running. + local not_started=() + + # 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 + # Initialize variables + # Check the OS and use the appropriate command + if [[ "$OSTYPE" == "linux-gnu"* ]]; then + if command -v ss > /dev/null 2>&1; then + info=$(ss -ltnp | grep ":$port" || true) + else + info=$(netstat -ltnp | grep ":$port" || true) + fi + elif [[ "$OSTYPE" == "darwin"* ]]; then + # For macOS, use lsof + info=$(lsof -P -i:"$port" | grep "LISTEN" || true) + fi + + # Check if any process is using the port + if [[ -z $info ]]; then + not_started+=($port) + else + if [[ "$OSTYPE" == "linux-gnu"* ]]; then + # Extract relevant details for Linux: Process Name, PID, and FD. + details=$(echo $info | sed -n 's/.*users:(("\([^"]*\)",pid=\([^,]*\),fd=\([^)]*\))).*/\1 \2 \3/p') + command=$(echo $details | awk '{print $1}') + pid=$(echo $details | awk '{print $2}') + fd=$(echo $details | awk '{print $3}') + elif [[ "$OSTYPE" == "darwin"* ]]; then + # Handle extraction for macOS + pid=$(echo $info | awk '{print $2}' | cut -d'/' -f1) + command=$(ps -p $pid -o comm= | xargs basename) + fd=$(echo $info | awk '{print $4}' | cut -d'/' -f1) + fi + + # Get the start time of the process using the PID + if [[ -z $pid ]]; then + start_time="N/A" + else + start_time=$(ps -p $pid -o lstart=) + fi + + started+=("Port $port - Command: $command, PID: $pid, FD: $fd, Started: $start_time") + fi + done + +# # Print information about ports whose processes are not running. +# if [[ ${#not_started[@]} -ne 0 ]]; then +# openim::log::info "\n### Not started ports:" +# for port in "${not_started[@]}"; do +# openim::log::error "Port $port is not started." +# done +# fi + + # Print information about ports whose processes are running. + if [[ ${#started[@]} -ne 0 ]]; then + openim::log::info "\n### No stop ports:" + for info in "${started[@]}"; do + openim::log::info "$info" + done + fi + + # If any of the processes is not running, return a status of 1. + if [[ ${#not_started[@]} -ne 0 ]]; then + openim::color::echo $COLOR_RED " OpenIM Stdout Log >> cat ${LOG_FILE}" + openim::color::echo $COLOR_RED " OpenIM Stderr Log >> cat ${STDERR_LOG_FILE}" + cat "$TMP_LOG_FILE" | awk '{print "\033[31m" $0 "\033[0m"}' + return 1 + else +# openim::log::success "All specified processes are running." + openim::log::success "Have processes no stop." + return 0 + fi +} + + # set +o errexit # Sample call for testing: # openim::util::check_ports 10002 1004 12345 13306