One instruction to solve the method of killing the specified port process under Linux and Windows (with Shell and Batch scripts)

foreword

In daily development and system administration, we often encounter situations where we need to terminate a process on a specific port. This article will introduce how to use a command to kill the process of the specified port under Linux and Windows systems, and will also provide corresponding Shell script and Batch script examples to achieve more convenient operations.

Solution under Linux:

In Linux systems, you can use killthe command to terminate a process on a specified port. The following is an example of a command combined with lsofthe command, which will find 8080the process of the specified port (for example) and kill it: ( : the following full root operation, non-root operation please add sudo by yourself)

kill -9 `lsof -ti:8080`

In this command, the process ID listening lsof -ti:8080on port will be found 8080, and then passed to kill -9the command to terminate the process. This method is simple and efficient, and can be done with one command.

Shell script:

If you want to encapsulate this operation into a shell script for repeated use, you can create a kill-port.shfile named with the following content:

#!/bin/bash

if [ $# -ne 1 ]; then
  echo "Usage: $0 <port>"
  exit 1
fi

port=$1
kill -9 $(lsof -ti :$port)

By executing chmod +x kill-port.shthe command, the script is given execution permission, and then ./kill-port.sh 8080such a command can be used to kill the process of the specified port.

The demonstration operation is as follows:

insert image description here
Check the occupancy of port 8080 through the above command lsof -i :8080. Here, LZ deliberately enters an extra parameter when using the script param2. The verification in the script ifwill prompt you how to use the script. After killing the process, use the command again lsof -i :8080to check whether port 8080 is killed

Solution under Windows:

In the Windows system, open it cmd(preferably run as an administrator), you can use netstatthe command to find the process ID corresponding to the port, and then use taskkillthe command to terminate the process. Here is an example command that combines the two commands to kill 8080a process on port:

for /f "tokens=5" %%a in ('netstat -ano ^| findstr :8080') do taskkill /F /PID %%a

Batch script:

Double-click the mouse to execute the script, the user enters the specified process port, press Enter and waits for three seconds to close cmdthe window automatically. For example, to kill 9060the port 步骤: (1. Create a txt file —> 2. Copy the following script content —> 3. Change the file suffix .bat—> double-click the script to execute it). The script content is as follows:

@echo off
chcp 65001

set/p port=kill-port:
echo port : %port%

set pid=
set executableFile=
for /f "usebackq tokens=1-5" %%a in (`netstat -ano ^| findstr %port%`) do (
    if [%%d] EQU [LISTENING] (
        set pid=%%e
    )
)

if "%pid%"=="" (
    echo 未找到监听端口为 %port% 的进程,无需终止。
) else (
    for /f "usebackq tokens=1-5" %%a in (`tasklist ^| findstr %pid%`) do (
        set executableFile=%%a
    )
    echo 现在将终止进程:pid %pid%,可执行文件:%executableFile%
    taskkill /f /pid %pid%
)

timeout /t 3 /nobreak > nul

chcp 65001Set the UTF-8 format in the current script to ensure that the Chinese characters are not garbled, timeoutcommand, so that the prompt information can be seen.

The demonstration operation is as follows:

  1. 成功终止操作显示如下
    insert image description here

  2. 无需终止操作显示如下
    insert image description here

Summarize:

Through the methods and examples introduced in this article, you can easily terminate the process of the specified port under Windows and Linux systems, which improves the efficiency and convenience of operations. Whether you execute a command directly on the command line or use a script to encapsulate the operation, you can manage the process more efficiently and ensure the normal operation of the system.

Guess you like

Origin blog.csdn.net/Da_zhenzai/article/details/132595516