Created
January 3, 2026 15:30
-
-
Save o-az/c99d525bec51bc17189cb28255ccc657 to your computer and use it in GitHub Desktop.
a SwiftBar-based macOS menu-bar app that displays open ports and lets you kill them
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # SwiftBar plugin: Open Listening Ports Monitor | |
| # Displays open TCP listening ports and lets you kill your own processes | |
| current_user=$(whoami) | |
| # Get listening ports with numeric values only | |
| open_ports=$(lsof -iTCP -sTCP:LISTEN -n -P 2>/dev/null | awk -v cu="$current_user" 'NR > 1 { | |
| user = $3 | |
| cmd = $1 | |
| pid = $2 | |
| port_field = $9 | |
| sub(/.*:/, "", port_field) # Extract everything after the last ":" | |
| sub(/ \(.*\)$/, "", port_field) # Remove trailing " (LISTEN)" | |
| if (port_field ~ /^[0-9]+$/) { | |
| print user "\t" cmd "\t" pid "\t" port_field | |
| } | |
| }' | sort -k4n) | |
| num_ports=$(echo "$open_ports" | grep -c . || echo 0) | |
| # Menu bar item | |
| echo "🔌 $num_ports" | |
| echo "---" | |
| echo "Refresh | refresh=true" | |
| if [ "$num_ports" -eq 0 ]; then | |
| echo "No open listening ports" | |
| exit 0 | |
| fi | |
| echo "---" | |
| # List each port | |
| echo "$open_ports" | while IFS=$'\t' read -r user cmd pid port; do | |
| if [ "$user" = "$current_user" ]; then | |
| echo "$port — $cmd (PID: $pid)" | |
| echo "--Kill gently (SIGTERM) | bash=/bin/kill param1=$pid terminal=false refresh=true" | |
| echo "--Force kill (SIGKILL) | bash=/bin/kill param1=-9 param2=$pid terminal=false refresh=true color=red" | |
| else | |
| echo "$port — $cmd (PID: $pid, user: $user) | color=gray" | |
| fi | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment