zoukankan      html  css  js  c++  java
  • linux根据端口号,或进程名获取进程pid

    1. 列出谁在使用某个端口

      lsof -i :{port}

      lsof(list open files)是一个列出当前系统打开文件的工具。在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件。
      [root@localhost webapps]# lsof -i:20880
      COMMAND  PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
      java    8624 root  297u  IPv6 14992707      0t0  TCP localhost.localdomain:20880 (LISTEN)
      java    8624 root  316u  IPv6 14990166      0t0  TCP localhost.localdomain:43330->localhost.localdomain:20880 (ESTABLISHED)
      java    8624 root  317u  IPv6 14990167      0t0  TCP localhost.localdomain:20880->localhost.localdomain:43330 (ESTABLISHED)
      java    8624 root  596u  IPv6 14990943      0t0  TCP localhost.localdomain:43386->localhost.localdomain:20880 (ESTABLISHED)
      java    8624 root  597u  IPv6 14990181      0t0  TCP localhost.localdomain:20880->localhost.localdomain:43386 (ESTABLISHED)
      java    8624 root  671u  IPv6 14991724      0t0  TCP localhost.localdomain:43392->localhost.localdomain:20880 (ESTABLISHED)
      java    8624 root  672u  IPv6 14990211      0t0  TCP localhost.localdomain:20880->localhost.localdomain:43392 (ESTABLISHED)
    2. netstat命令,求出进程PID,可进行其他操作,比如杀进程

      netstat -anp|grep {port}|awk '{printf $7}'|cut -d/ -f1

      [root@localhost webapps]# netstat -anp|grep 8080
      tcp6       0      0 :::8080                 :::*                    LISTEN      10027/java          
      [root@localhost webapps]# netstat -anp|grep 8080 |awk '{printf $7}'|cut -d/ -f1
      10027

      netstat -tunlp|grep {port}

      [root@localhost webapps]# netstat -tunlp|grep 8080
      tcp6       0      0 :::8080                 :::*                    LISTEN      10027/java          

       netstat -nlp | grep :{port}| awk '{print $7}' | awk -F"/" '{ print $1 }

      [root@localhost ~]# netstat -nlp | grep :8080
      tcp6       0      0 :::8080                 :::*                    LISTEN      27103/java          
      [root@localhost ~]# netstat -nlp | grep :8080 | awk '{print $7}' 
      27103/java
      [root@localhost ~]# netstat -nlp | grep :8080 | awk '{print $7}' | awk -F"/" '{ print $1 }'
      27103
      [root@localhost ~]# kill -9 $(netstat -nlp | grep :8088 | awk '{print $7}' | awk -F"/" '{ print $1 }') 
    3. 其他命令

      ps命令:  ps -ef | grep {programName}

      [root@localhost ~]# ps -ef | grep tomcat
      root     27103     1  1 11月21 ?      00:21:48 /usr/bin/java -Djava.util.logging.config.file=/usr/local/apache-tomcat-8.0.53/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Dspring.profiles.active=test -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dignore.endorsed.dirs= -classpath /usr/local/apache-tomcat-8.0.53/bin/bootstrap.jar:/usr/local/apache-tomcat-8.0.53/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/apache-tomcat-8.0.53 -Dcatalina.home=/usr/local/apache-tomcat-8.0.53 -Djava.io.tmpdir=/usr/local/apache-tomcat-8.0.53/temp org.apache.catalina.startup.Bootstrap start
      root     29611 29302  0 17:40 pts/0    00:00:00 grep --color=auto tomcat

      pgrep命令(pgrep命令的p表明了这个命令是专门用于进程查询的grep):

      pgrep {programName}   这个命令不知道为什么不管用,啧啧

    4. shell 杀进程

      pid0=$(netstat -nlp | grep :20880 | awk '{print $7}' | awk -F"/" '{ print $1 }');
      # 杀掉对应的进程,如果pid不存在,则不执行
      if [ -n "$pid0" ]; then
      kill -s 9 $pid0;
      fi

      另一种根据端口号杀进程的方法,根据更好用

      ps -ef | grep tomcat | grep -v grep | awk '{print $2}' | xargs kill -9
      [root@localhost ~]# lsof -i:20881
      COMMAND   PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
      java    27103 root  513u  IPv6 15892971      0t0  TCP localhost.localdomain:20881 (LISTEN)
      java    27103 root  520u  IPv6 15895638      0t0  TCP localhost.localdomain:60750->localhost.localdomain:20881 (ESTABLISHED)
      java    27103 root  521u  IPv6 15891070      0t0  TCP localhost.localdomain:20881->localhost.localdomain:60750 (ESTABLISHED)
      java    27103 root  609u  IPv6 15895639      0t0  TCP localhost.localdomain:60778->localhost.localdomain:20881 (ESTABLISHED)
      java    27103 root  610u  IPv6 15891079      0t0  TCP localhost.localdomain:20881->localhost.localdomain:60778 (ESTABLISHED)
      java    27103 root  756u  IPv6 15891110      0t0  TCP localhost.localdomain:60808->localhost.localdomain:20881 (ESTABLISHED)
      java    27103 root  757u  IPv6 15893007      0t0  TCP localhost.localdomain:20881->localhost.localdomain:60808 (ESTABLISHED)
      [root@localhost ~]# netstat -anp|grep 20881|awk '{printf $7}'|cut -d/ -f1 | xargs kill -9
      [root@localhost ~]# lsof -i:20881
      [root@localhost ~]# netstat -anp|grep 20881|awk '{printf $7}'|cut -d/ -f1 | xargs kill -9
      kill: 找不到进程“---”

    参考文章链接:

    1.   lsof命令具体用法   https://www.cnblogs.com/sparkbj/p/7161669.html
  • 相关阅读:
    Vim学习指南
    frambuffer lcd.c
    工控显示界面
    ubuntu nfs 开发板
    java初学1
    使用多态来实现数据库之间的切换
    Space Shooter 太空射击
    CandyCrush 糖果传奇
    进制转换以及原码、反码、补码
    winform小知识
  • 原文地址:https://www.cnblogs.com/season-qd/p/9972887.html
Copyright © 2011-2022 走看看