Skip to content →

Month: October 2018

Kill process running on port

Frequently I’ll run into the case where my IDE (Intellij, Xcode/Vapor) will fail and leave a web server running on a port. I can’t start it again because it is already running. The only way to proceed is to restart your computer or find and kill the process.

To find the a process running on a port you can use the lsof command. You can filter with “-i”, which lists IP sockets.

$ lsof -i tcp:8080

The command will produce output like:

COMMAND  PID   USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
Run     8299 ddiego   15u  IPv6 0xb2782dc730d0c751      0t0  TCP localhost:http-alt (LISTEN)

The PID is the process identification number. You can pass this to the kill command to kill the process:

$ kill -9 8299
Comments closed

U.S. Zip Code Validation in Swift

Here is a small function that will validate a US Zip Code using a Regexp with a: NSPredicate in Swift.

func validate(input: String?) -> Bool {
    guard let input = input else { return false }

    return NSPredicate(format: "SELF MATCHES %@", "^\\d{5}(?:[-\\s]?\\d{4})?$")
        .evaluate(with: input.uppercased())
}

Playground: zip-code-validation-playground

Comments closed