20 ms·
To support multiple PIDs: killport() { lsof -ti :"$1" | xargs kill -9; }
by tandav 3y ago
To support multiple PIDs:
killport() { lsof -ti :"$1" | xargs kill -9; }
- klysm 3y agoWhen would you have multiple pids?
- TheDong 3y agoSee SO_REUSEPORT in 'man 7 socket' If, for example, you run nginx in a default configuration, you'll notice both the master and worker processes will have an fd bound to the same port (port 80 or whatever), so it's also not exactly unusual. Even without SO_REUSEPORT, you can have multiple things on one port if you have multiple IPs, like something listening on "127.0.0.1:80", and a different process listening on "192.168.1.10:80"
- klysm 3y agoSo if you had one process listening to the same port on multiple ips, this xargs would fail on the second invocation? Seems like piping through uniq may be a good idea
- andylynch 3y agoProbably when binding a socket with SO_REUSEADDR or SO_REUSEPORT.
- whackx 3y agosshd does this. sshd 3891262 root 4u IPv4 15450342 0t0 TCP ibpmaas-testing:https->10.10.47.11:49064 (ESTABLISHED) sshd 3891351 ubuntu 4u IPv4 15450342 0t0 TCP ibpmaas-testing:https->10.10.47.11:49064 (ESTABLISHED) # lsof -ti ":49064" 3891262 3891351 edit for readability
- funcDropShadow 3y agokill accepts multiple pids. So there is no need to use xargs unless you expect so many pids that the command line is exhausted.
- xorcist 3y agokill already supports multiple pids kill $(lsof -t -i:TCP:$1 -sTCP:LISTEN) will do just fine. But you probably want listening ports only, hence -s. Unless you want it to work with millions of listening processes, which would overflow your environment and require the use of xargs. In that case you probably want GNU xargs with -n and -p to batch the job and run processes in parallel because that's going to take a while.