fix: fix the port check script

pull/1933/head
luhaoling 2 years ago
parent 42ad29bfdf
commit 50dec7bc4a

@ -53,5 +53,5 @@ func ExitWithError(err error) {
func SIGTERMExit() { func SIGTERMExit() {
progName := filepath.Base(os.Args[0]) progName := filepath.Base(os.Args[0])
fmt.Printf("\n\n%s receive process terminal SIGTERM exit 0\n\n", progName) fmt.Printf("%s receive process terminal SIGTERM exit 0", progName)
} }

@ -371,87 +371,61 @@ openim::util::check_ports() {
} }
openim::util::check_ports_by_signal() { openim::util::check_processes() {
# An array to collect ports of processes that are not running. # An array to collect information about processes that are still running.
local not_started=() local running=()
# An array to collect information about processes that are running. # An array to collect information about processes that have been stopped.
local started=() local stopped=()
openim::log::info "Checking ports: $*" openim::log::info "Checking processes for: $*"
# Iterate over each given port. # Iterate over each given process name.
for port in "$@"; do for name in "$@"; do
# Initialize variables # Check the OS and use the appropriate command to find processes by name
# Check the OS and use the appropriate command
if [[ "$OSTYPE" == "linux-gnu"* ]]; then if [[ "$OSTYPE" == "linux-gnu"* ]]; then
if command -v ss > /dev/null 2>&1; then info=$(pgrep -fl "$name" || true)
info=$(ss -ltnp | grep ":$port" || true) elif [[ "$OSTYPE" == "darwin"* ]]; then
else # For macOS, use pgrep as well, lsof for ports is not relevant here
info=$(netstat -ltnp | grep ":$port" || true) info=$(pgrep -fl "$name" || true)
fi
elif [[ "$OSTYPE" == "darwin"* ]]; then
# For macOS, use lsof
info=$(lsof -P -i:"$port" | grep "LISTEN" || true)
fi fi
# Check if any process is using the port # Check if the process is still running
if [[ -z $info ]]; then if [[ -z $info ]]; then
not_started+=($port) stopped+=("$name")
else else
if [[ "$OSTYPE" == "linux-gnu"* ]]; then running+=("Running process for $name: $info")
# 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 fi
done done
# # Print information about ports whose processes are not running. # Print information about processes that are still running.
# if [[ ${#not_started[@]} -ne 0 ]]; then if [[ ${#running[@]} -ne 0 ]]; then
# openim::log::info "\n### Not started ports:" openim::log::info "\n### Running processes:"
# for port in "${not_started[@]}"; do for info in "${running[@]}"; 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" openim::log::info "$info"
done done
else
# If all processes have been stopped, print a success message.
openim::log::success "All specified processes have been stopped."
fi fi
# If any of the processes is not running, return a status of 1. # Print information about processes that have been stopped, if any.
if [[ ${#not_started[@]} -ne 0 ]]; then if [[ ${#stopped[@]} -ne 0 ]]; then
openim::color::echo $COLOR_RED " OpenIM Stdout Log >> cat ${LOG_FILE}" openim::log::info "\n### Stopped processes:"
openim::color::echo $COLOR_RED " OpenIM Stderr Log >> cat ${STDERR_LOG_FILE}" for name in "${stopped[@]}"; do
cat "$TMP_LOG_FILE" | awk '{print "\033[31m" $0 "\033[0m"}' openim::log::info "Process $name has been stopped."
done
fi
# If any of the processes is still running, return a status of 1.
if [[ ${#running[@]} -ne 0 ]]; then
return 1 return 1
else else
# openim::log::success "All specified processes are running."
openim::log::success "Have processes no stop."
return 0 return 0
fi fi
} }
# set +o errexit # set +o errexit
# Sample call for testing: # Sample call for testing:
# openim::util::check_ports 10002 1004 12345 13306 # openim::util::check_ports 10002 1004 12345 13306

Loading…
Cancel
Save