diff --git a/bin/as.sh b/bin/as.sh index 73e2aa0e0..96256d0f1 100755 --- a/bin/as.sh +++ b/bin/as.sh @@ -635,14 +635,14 @@ parse_arguments() # check the process already using telnet port if equals to target pid if [[ ($telnetPortPid) && ($TARGET_PID != $telnetPortPid) ]]; then echo "[ERROR] Target process $TARGET_PID is not the process using port $TELNET_PORT, you will connect to an unexpected process." - echo "[ERROR] If you still want to attach target process $TARGET_PID, Try to set a different telnet port by using --telnet-port argument." - echo "[ERROR] Or try to shutdown the process $telnetPortPid using the telnet port first." + echo "[ERROR] 1. Try to restart as.sh, select process $telnetPortPid, shutdown it first." + echo "[ERROR] 2. Try to use different telnet port, for example: as.sh --telnet-port 9998 --http-port -1" exit 1 fi if [[ ($httpPortPid) && ($TARGET_PID != $httpPortPid) ]]; then echo "Target process $TARGET_PID is not the process using port $HTTP_PORT, you will connect to an unexpected process." - echo "If you still want to attach target process $TARGET_PID, Try to set a different telnet port by using --telnet-port argument." - echo "Or try to shutdown the process $httpPortPid using the telnet port first." + echo "1. Try to restart as.sh, select process $httpPortPid, shutdown it first." + echo "2. Try to use different http port, for example: as.sh --telnet-port 9998 --http-port 9999" exit 1 fi elif [ -z ${TARGET_PID} ]; then diff --git a/boot/src/main/java/com/taobao/arthas/boot/Bootstrap.java b/boot/src/main/java/com/taobao/arthas/boot/Bootstrap.java index 06f09d727..d9cade16a 100644 --- a/boot/src/main/java/com/taobao/arthas/boot/Bootstrap.java +++ b/boot/src/main/java/com/taobao/arthas/boot/Bootstrap.java @@ -271,7 +271,7 @@ public class Bootstrap { // select pid if (pid < 0) { try { - pid = ProcessUtils.select(bootstrap.isVerbose()); + pid = ProcessUtils.select(bootstrap.isVerbose(), telnetPortPid); } catch (InputMismatchException e) { System.out.println("Please input an integer to select pid."); System.exit(1); @@ -285,18 +285,18 @@ public class Bootstrap { if (telnetPortPid > 0 && pid != telnetPortPid) { AnsiLog.error("Target process {} is not the process using port {}, you will connect to an unexpected process.", pid, bootstrap.getTelnetPort()); - AnsiLog.error("If you still want to attach target process {}, Try to set a different telnet port by using --telnet-port argument.", - pid); - AnsiLog.error("Or try to shutdown the process {} using the telnet port first.", telnetPortPid); + AnsiLog.error("1. Try to restart arthas-boot, select process {}, shutdown it first.", + telnetPortPid); + AnsiLog.error("2. Or try to use different telnet port, for example: java -jar arthas-boot.jar --telnet-port 9998 --http-port -1"); System.exit(1); } if (httpPortPid > 0 && pid != httpPortPid) { AnsiLog.error("Target process {} is not the process using port {}, you will connect to an unexpected process.", pid, bootstrap.getHttpPort()); - AnsiLog.error("If you still want to attach target process {}, Try to set a different http port by using --http-port argument.", - pid); - AnsiLog.error("Or try to shutdown the process {} using the http port first.", httpPortPid); + AnsiLog.error("1. Try to restart arthas-boot, select process {}, shutdown it first.", + httpPortPid); + AnsiLog.error("2. Or try to use different http port, for example: java -jar arthas-boot.jar --telnet-port 9998 --http-port 9999", httpPortPid); System.exit(1); } diff --git a/boot/src/main/java/com/taobao/arthas/boot/ProcessUtils.java b/boot/src/main/java/com/taobao/arthas/boot/ProcessUtils.java index 6ff3bc0bf..d8cede5de 100644 --- a/boot/src/main/java/com/taobao/arthas/boot/ProcessUtils.java +++ b/boot/src/main/java/com/taobao/arthas/boot/ProcessUtils.java @@ -47,8 +47,17 @@ public class ProcessUtils { } @SuppressWarnings("resource") - public static int select(boolean v) throws InputMismatchException { + public static int select(boolean v, int telnetPortPid) throws InputMismatchException { Map processMap = listProcessByJps(v); + // Put the port that is already listening at the first + if (telnetPortPid > 0 && processMap.containsKey(telnetPortPid)) { + String telnetPortProcess = processMap.get(telnetPortPid); + processMap.remove(telnetPortPid); + Map newProcessMap = new LinkedHashMap(); + newProcessMap.put(telnetPortPid, telnetPortProcess); + newProcessMap.putAll(processMap); + processMap = newProcessMap; + } if (processMap.isEmpty()) { AnsiLog.info("Can not find java process. Try to pass in command line.");