Skip to content

Instantly share code, notes, and snippets.

@Overload119
Last active February 28, 2021 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Overload119/3cecbf8391d8bb7af635a608c0367e8e to your computer and use it in GitHub Desktop.
Save Overload119/3cecbf8391d8bb7af635a608c0367e8e to your computer and use it in GitHub Desktop.
Installation:
#!/usr/bin/env ruby
# Synced on Dropbox.
# Synced on https://gist.github.com/Overload119/3cecbf8391d8bb7af635a608c0367e8e
# cp ThisFile /usr/local/bin/close_port
running_apps = `lsof -i:#{ARGV[0]}`
return if running_apps.empty?
output_lines = running_apps.split("\n")
output_lines.drop(1).each do |line|
row = line.split(' ')
pid = row[1].to_i
app_name = row[0]
# Skip apps that should not be killed.
next if ARGV[1] && app_name != ARGV[1]
Process.kill('TERM', pid)
puts "TERM -> #{pid}"
fork do
sleep 1.5 # Expect the process to die after 1.5 seconds.
is_process_alive = Process.getpgid(pid) rescue false
if is_process_alive
Process.kill('KILL', pid)
puts "KILL -> #{pid}"
end
end
end
@Overload119
Copy link
Author

Usage close_port 1337 ruby

@Overload119
Copy link
Author

Installation

curl https://gist.githubusercontent.com/Overload119/3cecbf8391d8bb7af635a608c0367e8e/raw/d9da99b9f34a053eb13a8cb386fa2390b008ad79/close_port.rb >> /tmp/close_port.rb
sudo mv /tmp/close_port.rb /usr/local/bin/close_port
chmod +x /usr/local/bin/close_port

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