Kill processes interactively with FZF

I was tired of the tedious process of running ps aux | grep <process> and then manually copying PIDs to kill processes.

This bash function lets you interactively search and kill processes using the fuzzy-finding power of FZF, with support for both regular processes and port-specific killing.

Here's the code to add to your shell config (e.g., .bashrc or .zshrc):

1kport()
2{
3 local pids signal port_info
4 signal="15"
5
6 # Format strings and commands
7 local header_format="%-8s %-6s %-6s %-s %s\n"
8
9 local ps_filter='NR>1 && $11 !~ /^(\[|\/System\/|\/usr\/libexec\/)/ && $11 != "ps"'
10 # Filter: skip header, exclude system processes and ps command itself
11 local ps_printf='{printf "%-8s %-6.1f %-6.1f %-s %s\n", $2, $3, $4, $11, "N/A"}'
12 # Format: PID, CPU%, MEM%, COMMAND, PORT (N/A for regular processes)
13 local ps_awk_cmd="$ps_filter $ps_printf"
14
15 local lsof_awk_cmd='NR>1 {printf "%-8s %-6s %-6s %-s %s\n", $2, "N/A", "N/A", $1, $9}'
16 local fzf_header="Select process(es) to kill [Usage: kport or kport [port]]"
17
18 # Unified function to get process list
19 _get_process_list() {
20 {
21 printf "$header_format" "PID" "CPU%" "MEM%" "COMMAND" "PORT";
22 # Regular processes
23 ps aux | awk "$ps_awk_cmd" | sort -k2 -nr;
24 # Processes listening on ports
25 lsof -iTCP -sTCP:LISTEN -n -P 2>/dev/null | awk "$lsof_awk_cmd" | sort -k5;
26 }
27 }
28
29 # Unified function to kill processes
30 _kill_processes() {
31 local target_pids="$1"
32 local description="$2"
33
34 if [[ -n "$target_pids" ]]; then
35 echo "$description"
36 if echo "$target_pids" | xargs kill -"$signal" 2>/dev/null; then
37 echo "Successfully killed process(es)"
38 else
39 echo "Error: Failed to kill some processes. Try with signal 9." >&2
40 return 1
41 fi
42 fi
43 }
44
45 # Parse arguments: if first arg is a port number, kill processes on that port
46 if [[ -n "$1" && "$1" =~ ^[0-9]+$ ]]; then
47 # Filter process list for specific port
48 port_info=$(_get_process_list | grep -i ":$1$")
49 if [[ -z "$port_info" ]]; then
50 echo "No processes found listening on port $1"
51 return 1
52 fi
53
54 # Extract PIDs and kill
55 pids=$(echo "$port_info" | awk '{print $1}')
56 _kill_processes "$pids" "Processes listening on port $1:"
57 return
58 fi
59
60 # Interactive mode
61 pids=$(_get_process_list | fzf -m --header="$fzf_header" --header-lines=1 | awk '{print $1}')
62 _kill_processes "$pids" "Selected PIDs: $pids"
63}

How it works:

  1. Modular design: Uses internal helper functions _get_process_list() and _kill_processes() for cleaner code organization
  2. Port-specific killing: If you provide a port number as an argument, it filters the process list and kills processes listening on that specific port
  3. Interactive mode: Without arguments, shows a combined list of regular processes and listening ports via FZF
  4. Process filtering: Uses ps aux for regular processes and lsof for port-based processes, filtering out system processes
  5. Smart display: Shows PID, CPU%, MEM%, COMMAND, and PORT in a clean, columnated format
  6. CPU sorting: Sorts regular processes by CPU usage (highest first) for quick identification of resource hogs
  7. FZF Integration: Uses FZF for fuzzy searching with multi-select support (-m flag)
  8. Unified killing: Centralized process killing logic with consistent error handling and success messages

Usage examples:

1# Interactive mode - shows all processes and listening ports
2kport
3
4# Kill processes on specific port
5kport 3000 # Kill processes listening on port 3000
6kport 8080 # Kill processes listening on port 8080

Now, just type kport to pop up a searchable list of all your user processes and listening ports, or use kport <port> to quickly kill processes on a specific port. Find what you need to kill instantly and manage your system processes like a pro.

Kill processes interactively with FZF