How can I stop all processes in IntelliJ?

Viewed 40406

I am using intelliJ IDEA.

When I run my programs and close the window, the process still remains. If I run a lot of programs, I need to click disconnect many times.

Is there any way to stop all processes?

Eclipse doesn't have this problem.

PNG1

PNG2

4 Answers

You can create a script like killJavaProcess and invoke it in Before launch section as External tool

Before launch section

For example ~/.bin/killonport

#!/bin/zsh

function help() {
  echo "usage: killonport port"
  echo "-f don't ask before killing"
  echo "-h help"
}

function killListenerOnPort() {
  # get script options
  zparseopts -E -D h=HELP f=FORCE

  [[ -n $HELP ]] &&
    help &&
    return

  local PORT
  PORT=$1

  [[ -z "$PORT" ]] &&
    help &&
    return

  local LISTEN
  # find process listening on port with lsof
  LISTEN="$(lsof -nP -iTCP:"$PORT" -sTCP:LISTEN)"

  local PROCESS
  # remove first line of output lsof
  PROCESS="$(echo "$LISTEN" | tail -n +2)"

  local HEAD
  # get first line of output lsof
  HEAD="$(echo "$LISTEN" | head -n 1)"

  [[ -z $PROCESS ]] &&
    echo "Process not found" &&
    return

  local COUNT
  # assure that found only one process else printing founded processes and exit
  COUNT=$(echo "$PROCESS" | wc -l | xargs)
  [[ $COUNT -gt 1 ]] &&
    printf 'Found more then one process: %s\n%s\n%s\n' "$COUNT" "$HEAD" "$PROCESS" &&
    return

  # get name and PID
  local NAME
  NAME=$(echo "$PROCESS" | awk '{print $1}')
  local PID
  PID=$(echo "$PROCESS" | awk '{print $2}')

  # if -f option specified kill silently
  [[ -n "$FORCE" ]] &&
    kill -9 "$PID" &&
    return

  # ask confirmation to kill
  echo "Kill process '$NAME' with PID '$PID'"
  read -qr 'REPL?Continue? (y/n) '
  echo

  # kill if confirmed
  [[ $REPL == 'y' || $REPL == 'Y' ]] &&
    kill -9 "$PID"
}

killListenerOnPort "$@"
Related