magibney commented on code in PR #1328:
URL: https://github.com/apache/solr/pull/1328#discussion_r1095995278
##########
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:
I think it's fine to do a rough check that the process you get is likely a
java process. The more practical concern is `grep ":${port}"` and `grep 'LISTEN
'`-- ideally arguments could be provided that would directly achieve this
filtering as opposed to post-filtering unrestricted program output. E.g., maybe
some unfortunate person is running an executable program called "LISTEN" 😁. By
comparison, the args to the `lsof` command are explicit: we're getting
_exactly_ the pid, and it's exactly listening, exactly on the port we're
interested in. The grep stuff, though admittedly likely to work just fine, is a
lot fuzzier (even it it's probably be how I'd do troubleshooting on cmd-line,
as opposed to knowing the exact flags to these commands).
The only hesitance is: I'm not sure to what extent the args might vary
across different implementations of these commands, on different platforms.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]