switchon@lavalamp.biz
+27(0) 83 419 4851 / +27(0) 21 036 1165
Application & software development
Get A Quote

    • Home
    • Services
      • Application & software development
      • Outsourced software development
      • Project based resourcing
      • Digital marketing & consulting
      • Graphic design & consulting
      • UI / UX design & consulting
      • Recruitment services
      • Lease an expert
    • About
      • How we work
      • NBConsult Group
      • Partners
      • Lightbox Digital
    • Blog
    • Join us
    • Contact

    • Home
    • Services
      • Application & software development
      • Outsourced software development
      • Project based resourcing
      • Digital marketing & consulting
      • Graphic design & consulting
      • UI / UX design & consulting
      • Recruitment services
      • Lease an expert
    • About
      • How we work
      • NBConsult Group
      • Partners
      • Lightbox Digital
    • Blog
    • Join us
    • Contact

    • Home
    • Services
      • Application & software development
      • Outsourced software development
      • Project based resourcing
      • Digital marketing & consulting
      • Graphic design & consulting
      • UI / UX design & consulting
      • Recruitment services
      • Lease an expert
    • About
      • How we work
      • NBConsult Group
      • Partners
      • Lightbox Digital
    • Blog
    • Join us
    • Contact

    • Home
    • Services
      • Application & software development
      • Outsourced software development
      • Project based resourcing
      • Digital marketing & consulting
      • Graphic design & consulting
      • UI / UX design & consulting
      • Recruitment services
      • Lease an expert
    • About
      • How we work
      • NBConsult Group
      • Partners
      • Lightbox Digital
    • Blog
    • Join us
    • Contact
    scrum and agile methodology
    Scrum And Agile Methodology
    Mar 13, 2020
    errors in app insights
    Unhandled Promises Sent Through To App Insights In Node JS
    Jun 23, 2020

    A Kill Script For Processes That Are Using TCP/UDP Ports

    Categories
    • Software Development
    Tags
    • blocking
    • github
    • kill script
    • ports
    • processes
    • script
    kill scripts
    kill scripts

    I used to have trouble with processes blocking ports while developing. I would type up a number of commands trying to find the pid of the process that’s blocking the port and kill that process. It was rather frustrating. Eventually I took the commands and wrote a script that kills processes based on the port that it’s using.

    lsof -i:$1 | awk '{print $2}' | grep -v -e "PID" | sort -u > temp.txt
    pids=`cat temp.txt`
    rm temp.txt
    echo "The ids: $pids"
    for id in $pids; do
    echo "killing $id";
    kill -9 $id;
    done
    view raw kill_script_by_port.sh hosted with ❤ by GitHub

    You run it with ./kill_script_by_port.sh with the port number as an argument.

    The first line uses the lsof command to get the processes using that port. For example lsof -i:52639 outputs the following on my machine

    COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
    Code\x20H 6181 tielman 52u IPv6 0x570f09f514ea1165 0t0 TCP *:52639 (LISTEN)
    view raw Output_of_lsof52639 hosted with ❤ by GitHub

    This output gets piped into awk which gets the 2nd word of every line. grep removes the PID word. Finally sort -u removes any duplicate PIDs.

    The rest of the code reads the PIDs into an array and loops through them killing all the processes that use the port.

    If there’s a particular process you know you don’t want to kill, you can put the command for the process as an argument to grep -v -e before the awk in the pipeline. That process will be removed from the lsof output.

     

    Contact us


      Related posts:

      laravel livewireLaravel Livewire how to deploy a backend api to azureHow To Deploy A Backend API To Azure how to setup git-ftp for windowsHow To Setup Git-ftp For Windows implementing laravel echoImplementing Laravel Echo And Socket.io With A Laravel API And Ionic Vue App
      Share
      26
      Tielman Janse van Vuuren
      Tielman Janse van Vuuren
      Tielman is an experienced mobile and backend developer. Quality is his motto, no matter what programming language or platform.

      Leave a Reply Cancel reply

      Your email address will not be published. Required fields are marked *

      Lava Lamp Lab


      Like technology, a lava lamp constantly changes form, producing new conditions with every passing moment



      lava lamp lab facebook   lava lamp lab twitter   lava lamp lab linkedin   lava lamp lab instgram

      Services


      Application & software development

      Outsourced software development

      Project based resourcing

      Digital marketing & consulting

      Graphic design & consulting

      UI / UX design & consulting

      Contact Us


      +27(0) 83 419 4851

      +27(0) 21 036 1165


      switchon@lavalamp.biz


      Unit 4 Monaco Square,
      14 Church Street,
      Durbanville,
      Cape Town, 7550

      NBConsult Group


      nbconsult
      nbconnect msp
      nbclearning
      river broadband
      designer needed
      © 2023 Lava Lamp Lab (Pty) Ltd | All Rights Reserved | Privacy Policy
      Contact us now

        Application & software development

          Outsourced software development

            Project based resourcing

              Digital marketing & consulting

                Graphic design & consulting

                  UI/UX design & consulting

                    Lease an expert

                      Recruitment services

                        We are using cookies to give you the best experience on our website.

                        You can find out more about which cookies we are using or switch them off in settings.

                        Lava Lamp Lab
                        Powered by  GDPR Cookie Compliance
                        Privacy Overview

                        This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

                        Strictly Necessary Cookies

                        Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

                        If you disable this cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.

                        3rd Party Cookies

                        This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.

                        Keeping this cookie enabled helps us to improve our website.

                        Please enable Strictly Necessary Cookies first so that we can save your preferences!

                        Cookie Policy

                        More information about our Cookie Policy