Last active
December 4, 2025 06:11
-
-
Save anataliocs/2efc428386a4397ebc377b151e57a200 to your computer and use it in GitHub Desktop.
Search for and kill processes running on a certain port with a specific command name
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
| #!/bin/bash | |
| set -e | |
| port="9944" | |
| printf "\nSearches for processes running on port %s and performs a kill 9 on each pid" "$port" | |
| printf "\n------------------------- \n" | |
| printf "\nPrinting output from command: lsof -i :%s \n" "$port" | |
| lsof -i :$port | |
| printf "\nExtracting process ids..." | |
| printf "\n------------------------- \n" | |
| #Replace ink-node- with the name of the command | |
| ink_node_pids=$(lsof -i :$port | grep -oE 'ink-node-\s[0-9]{4,5}' | sed 's/ink-node-[[:space:]]//g' | xargs echo -n) | |
| printf "Found ink nodes running on the following process ids: %s \n" "$ink_node_pids" | |
| printf "\nExecuting command: %s for each pid \n" "kill -9" | |
| kill -9 $ink_node_pids |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output from command:
lsof -i :9944This script will extract the PID and run kill -9 on it.
In this example,
lsofis piped togrep -oE 'ink-node-\s[0-9]{4,5}'which grabs the string "ink-node- 20746"grepis piped tosed 's/ink-node-[[:space:]]//g'which removes the string "ink-node- " leaving just the process id 20746ink_node_pids$ink_node_pidsis then passed to the kill -9 command