We share a remote Linux box for developing various Java apps, and we use VisualVM over ssh to profile the apps as described here. Is there any way to enable JMX/profiling on our Java processes without needing to allocate/provision port numbers among our processes/users? It's annoying to have to always make sure you're specifying a (unique) port number just to enable profiling.
To make this all more concrete: hardcoding the port obviously doesn't work and will conflict:
exec java -Dcom.sun.management.jmxremote.port=3000 ...
We can require always specifying a unique port whenever you run a process, but this is tedious---you have to ensure your ports don't conflict with your other processes and also don't conflict with other users:
exec java -Dcom.sun.management.jmxremote.port=$1 ...
Currently we use:
exec java -Dcom.sun.management.jmxremote.port=$(( $RANDOM + 2000 )) ...
But we still occasionally bump into occupied port numbers.
We can continue with fancier scripts (e.g. querying netstat
for occupied port numbers and hoping there's no race), but we're wondering whether there's a better way / whether we're Doing It Wrong.