colvinco commented on code in PR #1328: URL: https://github.com/apache/solr/pull/1328#discussion_r1096112550
########## solr/bin/solr: ########## @@ -2158,6 +2158,35 @@ function mk_writable_dir() { fi } +# Check whether at least one of lsof/netstat/ss are available +function get_port_tool() { + for tool in 'lsof' 'netstat' 'ss'; do + which $tool &> nul + if [[ $? -eq 0 ]]; then + echo $tool + return + fi + done +} + +# Will get the PID of the java process running on the specified port, if one is running +# Otherwise empty result. Use get_port_tool to check whether lsof/ss/netstat is installed first +function check_port_in_use() { + local port=$1 + + case $(get_port_tool) in + lsof) + lsof -iTCP:${port} 2>/dev/null | grep "LISTEN" | grep -Eow "java\s*?[0-9]+" | grep -Eow [0-9]+ + ;; + ss) + ss -ntpa 2>/dev/null | grep ":${port} " | grep 'LISTEN ' | grep -Eow "\"java\",(pid=)?[0-9]+?" | grep -Eow "[0-9]+" + ;; + netstat) + netstat -nlp 2>/dev/null | grep ":${port} " | grep -Eow " [0-9]+/java" | grep -Eow "[0-9]+" + ;; Review Comment: Ah that's a pain. Yeah, on linux it gives the PID/program name column ``` # netstat -nlp 2>/dev/null Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:8983 0.0.0.0:* LISTEN 22862/java tcp 0 0 0.0.0.0:9983 0.0.0.0:* LISTEN 3380/java tcp 0 0 127.0.0.1:7983 0.0.0.0:* LISTEN 22862/java ``` https://apple.stackexchange.com/a/327967 suggests `-v` is the nearest alternative. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org For additional commands, e-mail: issues-h...@solr.apache.org