scripts for Mac

This commit is contained in:
skiffer-git 2024-03-19 15:10:19 +08:00
parent 27ad975359
commit 6e2132d217

View File

@ -382,81 +382,76 @@ openim::util::check_ports() {
# User: # User:
# openim::util::check_process_names nginx mysql redis # openim::util::check_process_names nginx mysql redis
# The function returns a status of 1 if any of the processes is not running. # The function returns a status of 1 if any of the processes is not running.
openim::util::check_process_names() { openim::util::check_process_names() {
# Function to get the port of a process # Function to get the port of a process
get_port() { get_port() {
local pid=$1 local pid=$1
if [[ "$OSTYPE" == "linux-gnu"* ]]; then case "$OSTYPE" in
# Linux "linux-gnu"*)
ss -ltnp 2>/dev/null | grep $pid | awk '{print $4}' | cut -d ':' -f2 # Linux
elif [[ "$OSTYPE" == "darwin"* ]]; then ss -ltnp 2>/dev/null | grep $pid | awk '{print $4}' | cut -d ':' -f2
# macOS ;;
lsof -nP -iTCP -sTCP:LISTEN -a -p $pid | awk 'NR>1 {print $9}' | sed 's/.*://' "darwin"*)
else # macOS
echo "Unsupported OS" lsof -nP -iTCP -sTCP:LISTEN -a -p $pid | awk 'NR>1 {print $9}' | sed 's/.*://'
return 1 ;;
fi *)
echo "Unsupported OS"
return 1
;;
esac
} }
# Arrays to collect details of processes # Arrays to collect process details
local not_started=() local not_started=()
local started=() local started=()
# Iterate over each given process name # Iterate over each given process name
for process_name in "$@"; do for process_name in "$@"; do
# Use `pgrep` to find process IDs related to the given process name local pids=($(pgrep -f "$process_name"))
local pids=($(pgrep -f $process_name))
# Check if any process IDs were found if [[ ${#pids[@]} -eq 0 ]]; then
if [[ ${#pids[@]} -eq 0 ]]; then not_started+=("$process_name")
not_started+=($process_name) else
else for pid in "${pids[@]}"; do
# If there are PIDs, loop through each one local command=$(ps -p $pid -o cmd=)
for pid in "${pids[@]}"; do local start_time=$(ps -p $pid -o lstart=)
local command=$(ps -p $pid -o cmd=) local port=$(get_port $pid)
local start_time=$(ps -p $pid -o lstart=) port=${port:-N/A}
local port=$(get_port $pid)
# Check if port information was found for the PID started+=("Process $process_name - Command: $command, PID: $pid, Port: $port, Start time: $start_time")
if [[ -z $port ]]; then done
port="N/A" fi
fi done
started+=("Process $process_name - Command: $command, PID: $pid, Port: $port, Start time: $start_time") # Print not started processes
done
fi
done
# Print information
if [[ ${#not_started[@]} -ne 0 ]]; then if [[ ${#not_started[@]} -ne 0 ]]; then
echo "Not started processes:" echo "Not started processes:"
for process_name in "${not_started[@]}"; do for process_name in "${not_started[@]}"; do
echo "Process $process_name is not started." echo " - Process $process_name is not started."
done done
fi fi
# Print started processes
if [[ ${#started[@]} -ne 0 ]]; then if [[ ${#started[@]} -ne 0 ]]; then
echo echo -e "\nStarted processes:"
echo "Started processes:"
for info in "${started[@]}"; do for info in "${started[@]}"; do
echo "$info" echo " - $info"
done done
fi fi
# Return status # Return status based on whether any processes have not started
if [[ ${#not_started[@]} -ne 0 ]]; then if [[ ${#not_started[@]} -ne 0 ]]; then
openim::color::echo $COLOR_RED "OpenIM Stdout Log >> cat ${LOG_FILE}" echo -e "\nSome processes have not started. Please check the logs for more information."
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 return 1
else else
echo "" echo -e "\nAll processes are running."
openim::log::success "All processes are running."
return 0 return 0
fi fi
} }
openim::util::check_process_names_for_stop() { openim::util::check_process_names_for_stop() {
# Function to get the port of a process # Function to get the port of a process
get_port() { get_port() {