From 9c143852ccaa51a948bb9312e4f6585912511037 Mon Sep 17 00:00:00 2001 From: skiffer-git <44203734@qq.com> Date: Wed, 6 Mar 2024 15:05:41 +0800 Subject: [PATCH 01/11] Optimize script logs --- scripts/install/openim-crontask.sh | 5 +++-- scripts/install/openim-msggateway.sh | 4 +++- scripts/install/openim-msgtransfer.sh | 5 +++-- scripts/install/openim-push.sh | 4 +++- scripts/install/openim-rpc.sh | 3 ++- scripts/lib/logging.sh | 7 ++++++- scripts/start-all.sh | 4 ++-- 7 files changed, 22 insertions(+), 10 deletions(-) diff --git a/scripts/install/openim-crontask.sh b/scripts/install/openim-crontask.sh index ae78a44dd..b8d57fed1 100755 --- a/scripts/install/openim-crontask.sh +++ b/scripts/install/openim-crontask.sh @@ -51,8 +51,9 @@ function openim::crontask::start() { openim::log::status "Start OpenIM Cron, path: ${OPENIM_CRONTASK_BINARY}" openim::log::status "start cron_task process, path: ${OPENIM_CRONTASK_BINARY}" - - nohup ${OPENIM_CRONTASK_BINARY} -c ${OPENIM_PUSH_CONFIG} >> ${LOG_FILE} 2> >(tee -a "${STDERR_LOG_FILE}" "$TMP_LOG_FILE" >&2) & + #nohup ${OPENIM_CRONTASK_BINARY} -c ${OPENIM_PUSH_CONFIG} >> ${LOG_FILE} 2> >(tee -a "${STDERR_LOG_FILE}" "$TMP_LOG_FILE" >&2) & + cmd= "${OPENIM_CRONTASK_BINARY} -c ${OPENIM_PUSH_CONFIG}" + nohup ${cmd} >> "${LOG_FILE}" 2> >(tee -a "$TMP_LOG_FILE" | while read line; do echo -e "\e[31m${line}\e[0m"; done >&2) >/dev/null & return 0 } diff --git a/scripts/install/openim-msggateway.sh b/scripts/install/openim-msggateway.sh index 3eb9bb349..25051aa6e 100755 --- a/scripts/install/openim-msggateway.sh +++ b/scripts/install/openim-msggateway.sh @@ -61,8 +61,10 @@ function openim::msggateway::start() { if [[ -n "${MSG_GATEWAY_PROM_PORTS_ARRAY[$i]}" ]]; then PROMETHEUS_PORT_OPTION="--prometheus_port ${MSG_GATEWAY_PROM_PORTS_ARRAY[$i]}" fi + cmd="${OPENIM_MSGGATEWAY_BINARY} --port ${OPENIM_MSGGATEWAY_PORTS_ARRAY[$i]} --ws_port ${OPENIM_WS_PORTS_ARRAY[$i]} $PROMETHEUS_PORT_OPTION -c ${OPENIM_MSGGATEWAY_CONFIG}" + nohup ${cmd} >> "${LOG_FILE}" 2> >(tee -a "$TMP_LOG_FILE" | while read line; do echo -e "\e[31m${line}\e[0m"; done >&2) >/dev/null & + # nohup ${OPENIM_MSGGATEWAY_BINARY} --port ${OPENIM_MSGGATEWAY_PORTS_ARRAY[$i]} --ws_port ${OPENIM_WS_PORTS_ARRAY[$i]} $PROMETHEUS_PORT_OPTION -c ${OPENIM_MSGGATEWAY_CONFIG} >> ${LOG_FILE} 2> >(tee -a "${STDERR_LOG_FILE}" "$TMP_LOG_FILE" >&2) & - nohup ${OPENIM_MSGGATEWAY_BINARY} --port ${OPENIM_MSGGATEWAY_PORTS_ARRAY[$i]} --ws_port ${OPENIM_WS_PORTS_ARRAY[$i]} $PROMETHEUS_PORT_OPTION -c ${OPENIM_MSGGATEWAY_CONFIG} >> ${LOG_FILE} 2> >(tee -a "${STDERR_LOG_FILE}" "$TMP_LOG_FILE" >&2) & done return 0 } diff --git a/scripts/install/openim-msgtransfer.sh b/scripts/install/openim-msgtransfer.sh index 9fdf07fe2..23ce79c36 100755 --- a/scripts/install/openim-msgtransfer.sh +++ b/scripts/install/openim-msgtransfer.sh @@ -57,10 +57,11 @@ function openim::msgtransfer::start() { openim::util::stop_services_on_ports ${PROMETHEUS_MSG_TRANSFER_PORT} PROMETHEUS_PORT_OPTION="--prometheus_port ${PROMETHEUS_MSG_TRANSFER_PORT}" fi - nohup ${OPENIM_MSGTRANSFER_BINARY} ${PROMETHEUS_PORT_OPTION} -c ${OPENIM_MSGTRANSFER_CONFIG} -n ${i} >> ${LOG_FILE} 2> >(tee -a "${STDERR_LOG_FILE}" "$TMP_LOG_FILE" >&2) & + cmd="${OPENIM_MSGTRANSFER_BINARY} ${PROMETHEUS_PORT_OPTION} -c ${OPENIM_MSGTRANSFER_CONFIG} -n ${i}" + nohup ${cmd} >> "${LOG_FILE}" 2> >(tee -a "$TMP_LOG_FILE" | while read line; do echo -e "\e[31m${line}\e[0m"; done >&2) >/dev/null & + #nohup ${OPENIM_MSGTRANSFER_BINARY} ${PROMETHEUS_PORT_OPTION} -c ${OPENIM_MSGTRANSFER_CONFIG} -n ${i} >> ${LOG_FILE} 2> >(tee -a "${STDERR_LOG_FILE}" "$TMP_LOG_FILE" >&2) & done return 0 - } function openim::msgtransfer::check() { diff --git a/scripts/install/openim-push.sh b/scripts/install/openim-push.sh index 8684f185c..8dea4b1f1 100755 --- a/scripts/install/openim-push.sh +++ b/scripts/install/openim-push.sh @@ -71,7 +71,9 @@ function openim::push::start() { for (( i=0; i<${#OPENIM_PUSH_PORTS_ARRAY[@]}; i++ )); do openim::log::info "start push process, port: ${OPENIM_PUSH_PORTS_ARRAY[$i]}, prometheus port: ${PUSH_PROM_PORTS_ARRAY[$i]}" - nohup ${OPENIM_PUSH_BINARY} --port ${OPENIM_PUSH_PORTS_ARRAY[$i]} -c ${OPENIM_PUSH_CONFIG} --prometheus_port ${PUSH_PROM_PORTS_ARRAY[$i]} >> ${LOG_FILE} 2> >(tee -a "${STDERR_LOG_FILE}" "$TMP_LOG_FILE" >&2) & + cmd="${OPENIM_PUSH_BINARY} --port ${OPENIM_PUSH_PORTS_ARRAY[$i]} -c ${OPENIM_PUSH_CONFIG} --prometheus_port ${PUSH_PROM_PORTS_ARRAY[$i]}" + nohup ${cmd} >> "${LOG_FILE}" 2> >(tee -a "$TMP_LOG_FILE" | while read line; do echo -e "\e[31m${line}\e[0m"; done >&2) >/dev/null & + #nohup ${OPENIM_PUSH_BINARY} --port ${OPENIM_PUSH_PORTS_ARRAY[$i]} -c ${OPENIM_PUSH_CONFIG} --prometheus_port ${PUSH_PROM_PORTS_ARRAY[$i]} >> ${LOG_FILE} 2> >(tee -a "${STDERR_LOG_FILE}" "$TMP_LOG_FILE" >&2) & done return 0 diff --git a/scripts/install/openim-rpc.sh b/scripts/install/openim-rpc.sh index e7ee430d2..c3ac12449 100755 --- a/scripts/install/openim-rpc.sh +++ b/scripts/install/openim-rpc.sh @@ -165,7 +165,8 @@ function openim::rpc::start_service() { printf "Specifying prometheus port: %s\n" "${prometheus_port}" cmd="${cmd} --prometheus_port ${prometheus_port}" fi - nohup ${cmd} >> "${LOG_FILE}" 2> >(tee -a "${STDERR_LOG_FILE}" "$TMP_LOG_FILE" >&2) & + #nohup ${cmd} >> "${LOG_FILE}" 2> >(tee -a "${STDERR_LOG_FILE}" "$TMP_LOG_FILE" >&2) & + nohup ${cmd} >> "${LOG_FILE}" 2> >(tee -a "$TMP_LOG_FILE" | while read line; do echo -e "\e[31m${line}\e[0m"; done >&2) >/dev/null & return 0 } diff --git a/scripts/lib/logging.sh b/scripts/lib/logging.sh index bef3b5961..8d017b077 100755 --- a/scripts/lib/logging.sh +++ b/scripts/lib/logging.sh @@ -237,4 +237,9 @@ function openim::log::test_log() { openim::log::error_exit "openim::log::error_exit" } -# openim::log::test_log \ No newline at end of file +# openim::log::test_log + +function openim::log::print_blue() { + echo -e "\033[0;36m$1\033[0m" +} + diff --git a/scripts/start-all.sh b/scripts/start-all.sh index 9a98bfca3..6bb41515c 100755 --- a/scripts/start-all.sh +++ b/scripts/start-all.sh @@ -69,7 +69,7 @@ fi "${OPENIM_ROOT}"/scripts/init-config.sh --skip -echo "You need to start the following scripts in order: ${OPENIM_SERVER_SCRIPTARIES[@]}" +openim::log::print_blue "Execute the following script in sequence: ${OPENIM_SERVER_SCRIPTARIES[@]}" # TODO Prelaunch tools, simple for now, can abstract functions later @@ -81,7 +81,7 @@ ${TOOLS_START_SCRIPTS_PATH} openim::tools::pre-start result=$("${OPENIM_ROOT}"/scripts/stop-all.sh) if [[ $? -ne 0 ]]; then - echo "+++ cat openim log file >>> ${LOG_FILE}" + openim::log::error "View the error logs from this startup. ${LOG_FILE} \n" openim::log::error "Some programs have not exited; the start process is aborted .\n $result" exit 1 fi From 44c3df4e8c4a07cd3bbd09c5f945c7eab8a2accb Mon Sep 17 00:00:00 2001 From: skiffer-git <44203734@qq.com> Date: Wed, 6 Mar 2024 15:12:35 +0800 Subject: [PATCH 02/11] Optimize script logs --- scripts/install/openim-crontask.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/install/openim-crontask.sh b/scripts/install/openim-crontask.sh index b8d57fed1..191701082 100755 --- a/scripts/install/openim-crontask.sh +++ b/scripts/install/openim-crontask.sh @@ -52,7 +52,7 @@ function openim::crontask::start() { openim::log::status "start cron_task process, path: ${OPENIM_CRONTASK_BINARY}" #nohup ${OPENIM_CRONTASK_BINARY} -c ${OPENIM_PUSH_CONFIG} >> ${LOG_FILE} 2> >(tee -a "${STDERR_LOG_FILE}" "$TMP_LOG_FILE" >&2) & - cmd= "${OPENIM_CRONTASK_BINARY} -c ${OPENIM_PUSH_CONFIG}" + cmd="${OPENIM_CRONTASK_BINARY} -c ${OPENIM_PUSH_CONFIG}" nohup ${cmd} >> "${LOG_FILE}" 2> >(tee -a "$TMP_LOG_FILE" | while read line; do echo -e "\e[31m${line}\e[0m"; done >&2) >/dev/null & return 0 From ff3b8d1d57c03751b4be6d921b9da0e975210f4a Mon Sep 17 00:00:00 2001 From: skiffer-git <44203734@qq.com> Date: Wed, 6 Mar 2024 15:12:39 +0800 Subject: [PATCH 03/11] Optimize script logs --- scripts/install/openim-api.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/install/openim-api.sh b/scripts/install/openim-api.sh index 8a7b8d1f1..cd3d5eb08 100755 --- a/scripts/install/openim-api.sh +++ b/scripts/install/openim-api.sh @@ -88,8 +88,8 @@ function openim::api::start_service() { echo "Starting service with command: $cmd" - nohup $cmd >> "${LOG_FILE}" 2> >(tee -a "${STDERR_LOG_FILE}" "$TMP_LOG_FILE" >&2) & - + #nohup $cmd >> "${LOG_FILE}" 2> >(tee -a "${STDERR_LOG_FILE}" "$TMP_LOG_FILE" >&2) & + nohup ${cmd} >> "${LOG_FILE}" 2> >(tee -a "$TMP_LOG_FILE" | while read line; do echo -e "\e[31m${line}\e[0m"; done >&2) >/dev/null & if [ $? -ne 0 ]; then openim::log::error_exit "Failed to start ${binary_name} on port ${service_port}." return 1 From 14bce90f14c2b66b39f4dc4319e6b9cafc6d658e Mon Sep 17 00:00:00 2001 From: skiffer-git <44203734@qq.com> Date: Wed, 6 Mar 2024 15:30:50 +0800 Subject: [PATCH 04/11] Optimize script logs --- scripts/lib/util.sh | 49 ++++++++++++++++++++++---------------------- scripts/start-all.sh | 2 -- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/scripts/lib/util.sh b/scripts/lib/util.sh index 4322255bb..bf636fd45 100755 --- a/scripts/lib/util.sh +++ b/scripts/lib/util.sh @@ -402,30 +402,31 @@ openim::util::check_process_names() { local not_started=() local started=() - # Iterate over each given process name - for process_name in "$@"; do - # 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 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=$(get_port $pid) - - # 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 +# Iterate over each given process name +for process_name in "$@"; do + # 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 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=$(get_port $pid | tr -d '\n') # Use `tr` to remove newline characters + + # 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 if [[ ${#not_started[@]} -ne 0 ]]; then diff --git a/scripts/start-all.sh b/scripts/start-all.sh index 6bb41515c..89f708ffa 100755 --- a/scripts/start-all.sh +++ b/scripts/start-all.sh @@ -95,7 +95,6 @@ sleep 2 result=$(. $(dirname ${BASH_SOURCE})/install/openim-msgtransfer.sh openim::msgtransfer::check) if [[ $? -ne 0 ]]; then - echo "+++ cat openim log file >>> ${LOG_FILE}" openim::log::error "The program may fail to start.\n $result" exit 1 fi @@ -103,7 +102,6 @@ fi result=$(openim::util::check_process_names ${OPENIM_ALL_SERVICE_LIBRARIES_NO_TRANSFER[@]}) if [[ $? -ne 0 ]]; then - echo "+++ cat openim log file >>> ${LOG_FILE}" openim::log::error "The program may fail to start.\n $result" exit 1 fi From 422c01870a51b863a1ae28fa048c7b9e03fadb80 Mon Sep 17 00:00:00 2001 From: skiffer-git <44203734@qq.com> Date: Wed, 6 Mar 2024 15:33:38 +0800 Subject: [PATCH 05/11] Optimize script logs --- scripts/lib/util.sh | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/scripts/lib/util.sh b/scripts/lib/util.sh index bf636fd45..66e96c1dd 100755 --- a/scripts/lib/util.sh +++ b/scripts/lib/util.sh @@ -409,17 +409,19 @@ for process_name in "$@"; do # Check if any process IDs were found if [[ ${#pids[@]} -eq 0 ]]; then - not_started+=($process_name) + not_started+=("$process_name") else # 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=$(get_port $pid | tr -d '\n') # Use `tr` to remove newline characters + local port=$(get_port $pid | tr -d '\n') # Remove newline characters - # Check if port information was found for the PID + # Ensure port information is followed by a space for separation if [[ -z $port ]]; then - port="N/A" + port="N/A " + else + port="$port " # Add a trailing space for separation fi started+=("Process $process_name - Command: $command, PID: $pid, Port: $port, Start time: $start_time") From 74802c601c08930fa0b1c3068620910b3d62d4a4 Mon Sep 17 00:00:00 2001 From: skiffer-git <44203734@qq.com> Date: Wed, 6 Mar 2024 15:36:28 +0800 Subject: [PATCH 06/11] Optimize script logs --- scripts/lib/util.sh | 55 ++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/scripts/lib/util.sh b/scripts/lib/util.sh index 66e96c1dd..05904198d 100755 --- a/scripts/lib/util.sh +++ b/scripts/lib/util.sh @@ -401,33 +401,36 @@ openim::util::check_process_names() { # Arrays to collect details of processes local not_started=() local started=() - -# Iterate over each given process name -for process_name in "$@"; do - # 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 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=$(get_port $pid | tr -d '\n') # Remove newline characters - - # Ensure port information is followed by a space for separation - if [[ -z $port ]]; then - port="N/A " - else - port="$port " # Add a trailing space for separation - fi - started+=("Process $process_name - Command: $command, PID: $pid, Port: $port, Start time: $start_time") - done - fi -done + # Iterate over each given process name + for process_name in "$@"; do + # 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 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=$(get_port $pid | tr -d '\n') # Remove newline characters + + # Insert a space at the desired position in the port string + if [[ ! -z $port && $port != "N/A" ]]; then + port="${port:0:4} ${port:4}" # Add a space after the fourth character + fi + + 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 From ae75708e9237c40453e3fd757831bc392984d2e1 Mon Sep 17 00:00:00 2001 From: skiffer-git <44203734@qq.com> Date: Wed, 6 Mar 2024 15:39:11 +0800 Subject: [PATCH 07/11] Optimize script logs --- scripts/lib/util.sh | 56 +++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/scripts/lib/util.sh b/scripts/lib/util.sh index 05904198d..765405fed 100755 --- a/scripts/lib/util.sh +++ b/scripts/lib/util.sh @@ -402,34 +402,36 @@ openim::util::check_process_names() { local not_started=() local started=() - # Iterate over each given process name - for process_name in "$@"; do - # 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 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=$(get_port $pid | tr -d '\n') # Remove newline characters - - # Insert a space at the desired position in the port string - if [[ ! -z $port && $port != "N/A" ]]; then - port="${port:0:4} ${port:4}" # Add a space after the fourth character - fi + # Iterate over each given process name + for process_name in "$@"; do + # 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 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=$(get_port $pid | tr -d '\n') # Remove any newline characters + + # Add space within port numbers, assuming you need to format them + if [[ ! -z $port && $port != "N/A" ]]; then + # Example formatting: assuming ports are returned as a single long string and you want to separate every 4 characters with a space + port=$(echo "$port" | sed 's/.\{4\}/& /g') + fi + + 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 - 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 From f540b77e78fda0adef0fadd25efa388e92ac3cf3 Mon Sep 17 00:00:00 2001 From: skiffer-git <44203734@qq.com> Date: Wed, 6 Mar 2024 15:43:26 +0800 Subject: [PATCH 08/11] Optimize script logs --- scripts/lib/util.sh | 73 +++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/scripts/lib/util.sh b/scripts/lib/util.sh index 765405fed..f647284be 100755 --- a/scripts/lib/util.sh +++ b/scripts/lib/util.sh @@ -384,19 +384,20 @@ openim::util::check_ports() { # The function returns a status of 1 if any of the processes is not running. openim::util::check_process_names() { # Function to get the port of a process - get_port() { - local pid=$1 - if [[ "$OSTYPE" == "linux-gnu"* ]]; then - # Linux - ss -ltnp 2>/dev/null | grep $pid | awk '{print $4}' | cut -d ':' -f2 - elif [[ "$OSTYPE" == "darwin"* ]]; then - # macOS - lsof -nP -iTCP -sTCP:LISTEN -a -p $pid | awk 'NR>1 {print $9}' | sed 's/.*://' - else - echo "Unsupported OS" - return 1 - fi - } + get_port() { + local pid=$1 + if [[ "$OSTYPE" == "linux-gnu"* ]]; then + # Linux + ss -ltnp 2>/dev/null | grep $pid | awk '{print $4}' | cut -d ':' -f2 | tr '\n' ' ' + elif [[ "$OSTYPE" == "darwin"* ]]; then + # macOS + lsof -nP -iTCP -sTCP:LISTEN -a -p $pid | awk 'NR>1 {print $9}' | sed 's/.*://' | tr '\n' ' ' + else + echo "Unsupported OS" + return 1 + fi + } + # Arrays to collect details of processes local not_started=() @@ -1687,29 +1688,29 @@ openim::util::check_process_names() { echo "Checking processes: $*" # Iterate over each given process name - for process_name in "$@"; do - # 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 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=$(get_port $pid) - - # 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 + for process_name in "$@"; do + # 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 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=$(get_port $pid) + + # 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 if [[ ${#not_started[@]} -ne 0 ]]; then From 406228297e733d48300419d72a1831420edee8b3 Mon Sep 17 00:00:00 2001 From: skiffer-git <44203734@qq.com> Date: Wed, 6 Mar 2024 15:44:00 +0800 Subject: [PATCH 09/11] Optimize script logs --- scripts/lib/util.sh | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/scripts/lib/util.sh b/scripts/lib/util.sh index f647284be..4ca796beb 100755 --- a/scripts/lib/util.sh +++ b/scripts/lib/util.sh @@ -384,20 +384,19 @@ openim::util::check_ports() { # The function returns a status of 1 if any of the processes is not running. openim::util::check_process_names() { # Function to get the port of a process - get_port() { - local pid=$1 - if [[ "$OSTYPE" == "linux-gnu"* ]]; then - # Linux - ss -ltnp 2>/dev/null | grep $pid | awk '{print $4}' | cut -d ':' -f2 | tr '\n' ' ' - elif [[ "$OSTYPE" == "darwin"* ]]; then - # macOS - lsof -nP -iTCP -sTCP:LISTEN -a -p $pid | awk 'NR>1 {print $9}' | sed 's/.*://' | tr '\n' ' ' - else - echo "Unsupported OS" - return 1 - fi - } - + get_port() { + local pid=$1 + if [[ "$OSTYPE" == "linux-gnu"* ]]; then + # Linux + ss -ltnp 2>/dev/null | grep $pid | awk '{print $4}' | cut -d ':' -f2 + elif [[ "$OSTYPE" == "darwin"* ]]; then + # macOS + lsof -nP -iTCP -sTCP:LISTEN -a -p $pid | awk 'NR>1 {print $9}' | sed 's/.*://' + else + echo "Unsupported OS" + return 1 + fi + } # Arrays to collect details of processes local not_started=() From b85bb4ca4b4706a171c2e1cf865e3fbf1c1847b9 Mon Sep 17 00:00:00 2001 From: skiffer-git <44203734@qq.com> Date: Wed, 6 Mar 2024 15:45:36 +0800 Subject: [PATCH 10/11] Optimize script logs --- scripts/lib/util.sh | 51 ++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/scripts/lib/util.sh b/scripts/lib/util.sh index 4ca796beb..8903d083e 100755 --- a/scripts/lib/util.sh +++ b/scripts/lib/util.sh @@ -403,34 +403,29 @@ openim::util::check_process_names() { local started=() # Iterate over each given process name - for process_name in "$@"; do - # 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 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=$(get_port $pid | tr -d '\n') # Remove any newline characters - - # Add space within port numbers, assuming you need to format them - if [[ ! -z $port && $port != "N/A" ]]; then - # Example formatting: assuming ports are returned as a single long string and you want to separate every 4 characters with a space - port=$(echo "$port" | sed 's/.\{4\}/& /g') - fi - - 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 + for process_name in "$@"; do + # 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 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=$(get_port $pid) + + # 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 From 2041c662c4b7825f910f63e0686f62ec76c14924 Mon Sep 17 00:00:00 2001 From: skiffer-git <44203734@qq.com> Date: Wed, 6 Mar 2024 15:54:53 +0800 Subject: [PATCH 11/11] Optimize script logs --- scripts/lib/util.sh | 2 -- scripts/start-all.sh | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/scripts/lib/util.sh b/scripts/lib/util.sh index 8903d083e..d84562c1d 100755 --- a/scripts/lib/util.sh +++ b/scripts/lib/util.sh @@ -427,8 +427,6 @@ openim::util::check_process_names() { fi done - - # Print information if [[ ${#not_started[@]} -ne 0 ]]; then diff --git a/scripts/start-all.sh b/scripts/start-all.sh index 89f708ffa..c27f2010c 100755 --- a/scripts/start-all.sh +++ b/scripts/start-all.sh @@ -37,7 +37,7 @@ function execute_start_scripts() { # Check if the script file exists and is executable. if [[ -x "$script_path" ]]; then - openim::log::info "Starting script: ${script_path##*/}" # Log the script name. + openim::log::print_blue "Starting script: ${script_path##*/}" # Log the script name. # Execute the script with the constructed argument. result=$("$script_path" "$arg") @@ -69,7 +69,7 @@ fi "${OPENIM_ROOT}"/scripts/init-config.sh --skip -openim::log::print_blue "Execute the following script in sequence: ${OPENIM_SERVER_SCRIPTARIES[@]}" +#openim::log::print_blue "Execute the following script in sequence: ${OPENIM_SERVER_SCRIPTARIES[@]}" # TODO Prelaunch tools, simple for now, can abstract functions later