Last active
February 28, 2021 22:56
-
-
Save Overload119/3cecbf8391d8bb7af635a608c0367e8e to your computer and use it in GitHub Desktop.
Installation:
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 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 |
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
Usage
close_port 1337 ruby