Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

Using Lsof To Find Current Running Process For A Given Port

12/22/2016

0 Comments

 
I stumbled across this answer while looking at this stack overflow post. I thought it was such a great solution to my problem that I decided to write a blog post about it to help me not forget it.

Lsof To The Rescue

The linux command lsof stands for "list open files", and it's useful in this situation because it lists which files are open and by which processes. 
lsof

Filtering For A Given Port

If you just type in lsof into your command line and hit enter you might have a heart attack from the amount of information that gets dumped into the command propmt. Let's filter some stuff out so that it only shows us what we care about. In this case I only care about processes running on port 4200 (yep, it's an Angular 2 CLI app. hehe). We can use the ​-i flag with lsof to show only files open for a given port:
lsof -i :4200

Why the Colon?

Notice the colon prefixing the port number above. It would result in an error if you tried to run "lsof -i 4200", and the error would be, "lsof: unknown protocol name (200) in: -i 4200". We need to specify some protocol like TCP, UDP, etc. In my case I'm actually using TCP protocol so this command would work as well:
lsof -i TCP:4200
The trick is that if you don't know the protocol and want to look up processes running on a given port for any protocol then you can just leave off the protocol but keep the colon, resulting is a sort of shortcut wildcard syntax.
Picture
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Want FREE access to
    my daily stock tips 
    ​newsletters??
    Sign up here:

    - Triple Gainers
    - Rippers N' Dippers
    - Growingest Growers
    ​

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    February 2021
    January 2021
    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2021