Skip to content

Instantly share code, notes, and snippets.

@anataliocs
Last active December 4, 2025 06:11
Show Gist options
  • Select an option

  • Save anataliocs/2efc428386a4397ebc377b151e57a200 to your computer and use it in GitHub Desktop.

Select an option

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
#!/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
@anataliocs
Copy link
Author

anataliocs commented Dec 3, 2025

Output from command: lsof -i :9944

COMMAND     PID          USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
ink-node- 20746 chrisanatalio   32u  IPv4 0xb3e07a7b42b1f354      0t0  TCP localhost:9944 (LISTEN)

This script will extract the PID and run kill -9 on it.

In this example,

  • The output of lsof is piped to grep -oE 'ink-node-\s[0-9]{4,5}' which grabs the string "ink-node- 20746"
  • Then the string "ink-node- 20746" from the grep is piped to sed 's/ink-node-[[:space:]]//g' which removes the string "ink-node- " leaving just the process id 20746
  • These process ids are then stored in a var ink_node_pids
  • The var $ink_node_pids is then passed to the kill -9 command

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment