如何在Window或Linux/Unix中查找并干掉占用指定端口的进程

Windows

netstat /?

Displays protocol statistics and current TCP/IP network connections.

NETSTAT [-a] [-b] [-e] [-n] [-o] [-p proto] [-r] [-s] [-v] [interval]

  -a            Displays all connections and listening ports.
  -b            Displays the executable involved in creating each connection or
                listening port. In some cases well-known executables host
                multiple independent components, and in these cases the
                sequence of components involved in creating the connection
                or listening port is displayed. In this case the executable
                name is in [] at the bottom, on top is the component it called,
                and so forth until TCP/IP was reached. Note that this option
                can be time-consuming and will fail unless you have sufficient
                permissions.
  -e            Displays Ethernet statistics. This may be combined with the -s
                option.
  -n            Displays addresses and port numbers in numerical form.
  -o            Displays the owning process ID associated with each connection.
  -p proto      Shows connections for the protocol specified by proto; proto
                may be any of: TCP, UDP, TCPv6, or UDPv6.  If used with the -s
                option to display per-protocol statistics, proto may be any of:
                IP, IPv6, ICMP, ICMPv6, TCP, TCPv6, UDP, or UDPv6.
  -r            Displays the routing table.
  -s            Displays per-protocol statistics.  By default, statistics are
                shown for IP, IPv6, ICMP, ICMPv6, TCP, TCPv6, UDP, and UDPv6;
                the -p option may be used to specify a subset of the default.
  -v            When used in conjunction with -b, will display sequence of
                components involved in creating the connection or listening
                port for all executables.
  interval      Redisplays selected statistics, pausing interval seconds
                between each display.  Press CTRL+C to stop redisplaying
                statistics.  If omitted, netstat will print the current
                configuration information once.

netstat -o

  Proto  Local Address          Foreign Address        State           PID
  ...
  TCP    IXX-L3YWERK:3521       localhost:3306         ESTABLISHED     5892
  TCP    IXX-L3YWERK:3522       localhost:3306         ESTABLISHED     5892
  TCP    IXX-L3YWERK:3524       localhost:3306         ESTABLISHED     5892
  TCP    IXX-L3YWERK:3525       localhost:3306         ESTABLISHED     5892
  TCP    IXX-L3YWERK:3526       localhost:3306         ESTABLISHED     5892
  TCP    IXX-L3YWERK:3527       localhost:3306         ESTABLISHED     5892
  TCP    IXX-L3YWERK:3528       localhost:3306         ESTABLISHED     5892
  TCP    IXX-L3YWERK:3529       localhost:3306         ESTABLISHED     5892
  TCP    IXX-L3YWERK:3530       localhost:3306         ESTABLISHED     5892
  ...

taskkill /?

TASKKILL [/S system [/U username [/P [password]]]]
         { [/FI filter] [/PID processid | /IM imagename] } [/F] [/T]

Description:
    This command line tool can be used to end one or more processes.
    Processes can be killed by the process id or image name.

Parameter List:
    /S    system           Specifies the remote system to connect to.

    /U    [domain/]user    Specifies the user context under which
                           the command should execute.

    /P    [password]       Specifies the password for the given
                           user context. Prompts for input if omitted.

    /F                     Specifies to forcefully terminate
                           process(es).

    /FI   filter           Displays a set of tasks that match a
                           given criteria specified by the filter.

    /PID  process id       Specifies the PID of the process that
                           has to be terminated.

    /IM   image name       Specifies the image name of the process
                           that has to be terminated. Wildcard '*'
                           can be used to specify all image names.

    /T                     Tree kill: terminates the specified process
                           and any child processes which were started by it.

    /?                     Displays this help/usage.

Filters:
    Filter Name   Valid Operators           Valid Value(s)
    -----------   ---------------           --------------
    STATUS        eq, ne                    RUNNING | NOT RESPONDING
    IMAGENAME     eq, ne                    Image name
    PID           eq, ne, gt, lt, ge, le    PID value
    SESSION       eq, ne, gt, lt, ge, le    Session number.
    CPUTIME       eq, ne, gt, lt, ge, le    CPU time in the format
                                            of hh:mm:ss.
                                            hh - hours,
                                            mm - minutes, ss - seconds
    MEMUSAGE      eq, ne, gt, lt, ge, le    Memory usage in KB
    USERNAME      eq, ne                    User name in [domain/]user
                                            format
    MODULES       eq, ne                    DLL name
    SERVICES      eq, ne                    Service name
    WINDOWTITLE   eq, ne                    Window title

NOTE: Wildcard '*' for the /IM switch is accepted only with filters.

NOTE: Termination of remote processes will always be done forcefully
      irrespective of whether /F option is specified or not.

Examples:
    TASKKILL /S system /F /IM notepad.exe /T
    TASKKILL /PID 1230 /PID 1241 /PID 1253 /T
    TASKKILL /F /IM notepad.exe /IM mspaint.exe
    TASKKILL /F /FI "PID ge 1000" /FI "WINDOWTITLE ne untitle*"
    TASKKILL /F /FI "USERNAME eq NT AUTHORITY/SYSTEM" /IM notepad.exe
    TASKKILL /S system /U domain/username /FI "USERNAME ne NT*" /IM *
    TASKKILL /S system /U username /P password /FI "IMAGENAME eq note*"

除了使用taskkill外,还可以通过快捷键Ctrl + Shift + Esc打开任务管理器,在它的“Process”或“进程”页签下找到对应的PID来结束任务!

Linux/Unix

netstat --help

usage: netstat [-vWeenNcCF] [] -r         netstat {-V|--version|-h|--help}
       netstat [-vWnNcaeol] [ ...]
       netstat { [-vWeenNac] -i | [-cWnNe] -M | -s }

        -r, --route              display routing table
        -i, --interfaces         display interface table
        -g, --groups             display multicast group memberships
        -s, --statistics         display networking statistics (like SNMP)
        -M, --masquerade         display masqueraded connections

        -v, --verbose            be verbose
        -W, --wide               don't truncate IP addresses
        -n, --numeric            don't resolve names
        --numeric-hosts          don't resolve host names
        --numeric-ports          don't resolve port names
        --numeric-users          don't resolve user names
        -N, --symbolic           resolve hardware names
        -e, --extend             display other/more information
        -p, --programs           display PID/Program name for sockets
        -c, --continuous         continuous listing

        -l, --listening          display listening server sockets
        -a, --all, --listening   display all sockets (default: connected)
        -o, --timers             display timers
        -F, --fib                display Forwarding Information Base (default)
        -C, --cache              display routing cache instead of FIB

  ={-t|--tcp} {-u|--udp} {-w|--raw} {-x|--unix} --ax25 --ipx --netrom
  =Use '-6|-4' or '-A ' or '--'; default: inet
  List of possible address families (which support routing):
    inet (DARPA Internet) inet6 (IPv6) ax25 (AMPR AX.25)
    netrom (AMPR NET/ROM) ipx (Novell IPX) ddp (Appletalk DDP)
    x25 (CCITT X.25)

netstat -lp

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
...
tcp        0      0 *:amandaidx             *:*                     LISTEN      -
tcp        0      0 localhost:amidxtape     *:*                     LISTEN      -
tcp        0      0 *:8808                  *:*                     LISTEN      -
tcp        0      0 PKG-LOCAL-SERVER.:mysql *:*                     LISTEN      -
tcp        0      0 localhost:submission    *:*                     LISTEN      -
tcp        0      0 *:netbios-ssn           *:*                     LISTEN      -
tcp        0      0 *:www                   *:*                     LISTEN      -
tcp        0      0 *:50001                 *:*                     LISTEN      -
tcp        0      0 *:ssh                   *:*                     LISTEN      -
tcp        0      0 localhost:ipp           *:*                     LISTEN      -
tcp        0      0 localhost:smtp          *:*                     LISTEN      -
...

kill

kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]

使用man kill可以取得详细说明

你可能感兴趣的:(window)