Ives
Ives

Reputation: 555

How can I stop all processes in IntelliJ?

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

Upvotes: 19

Views: 58903

Answers (5)

KLMN
KLMN

Reputation: 559

My environment is Windows, and I am using IntelliJ version 2024.1.2 (Community Edition). IntelliJ often shows that the port is blocked by another application when I restart my Spring Boot application. I resolved this problem by using a shell script to kill the process that occupies the port and adding it to the 'Add Before Launch Task' section of the run configuration.

IntelliJ Add Before Launch Task

enter image description here

kill_port_8081.bat:

@echo off
rem Check the port 8081 and kill the process using it

echo Looking for process using port 8081...
for /f "tokens=5" %%a in ('netstat -aon ^| findstr 8081') do (
    echo Process found using port 8081: PID %%a
    echo Killing process %%a...
    taskkill /F /PID %%a
    echo Process %%a killed.
)

echo Done.
pause

Upvotes: 0

Matthew Read
Matthew Read

Reputation: 1860

IntelliJ 2017.2 now has a "Stop All" button in the "Stop process" menu (the button on the top bar), with the default shortcut +F2 on Mac OS:

screenshot

For older versions:

  1. Click the Stop button from the top bar. It will pop open a menu listing all processes. (The stop button at the side of the debug window is per-process, as in your screenshot.)

  2. Hover over the first process, hold Shift, and then click on the last process.

  3. Press Enter.

Screenshot showing the result of steps 1 & 2:

screenshot

Upvotes: 11

Alex Po
Alex Po

Reputation: 2035

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 "$@"

Upvotes: 2

haugan
haugan

Reputation: 405

Not exactly perfect, but what you could do is press Ctrl + F2 (shortcut for Stop Process) and hit Enter. It's better than all that mouse clicking and gets you through a list of running processes quite fast.

Upvotes: 2

CJ Jacobs
CJ Jacobs

Reputation: 299

kill $(ps aux | grep 'java' | awk '{print $2}')

This is a nice little workaround I found on SO a while ago that will kill any process with "java" in the name.

Just copy and paste into the terminal.

Upvotes: 8

Related Questions