Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Shell script to kill port occupancy with one click

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how to use the Shell script to kill the port occupation with one click. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

1. Preface

In Web development, we often encounter the scenario of "port occupied".

The general solution is:

Use the lsof-I command to query the process PID that occupies the port

Use kill-9 PID to kill the target process

Although there are only two steps, I also find it tedious, that is, you have to remember the command and type it twice.

This article will write a Shell script to kill port occupancy with one click

two。 Realize

Compared with the Python,Shell script, which is often used to deal with the business at the bottom of the operating system, it is simple and efficient.

Take Mac OSX as an example, create a .sh file locally, and the steps are as follows:

2-1 define port number and filter content parameters

Parameterize the port number to be processed and the command line filter content, and then assign values to two variables in the Shell script

, 1

The first parameter to execute the script, that is, the port number

$2

Execute the second parameter of the script, that is, filter the contents of the process command

# Port number to kill port_be_kill=$1 # filter content filter_content=$2

2-2 get the return value of the port occupancy process

Use the port number to form the lsof-I command, execute this command, and assign the return value to the variable ip_status

# get the Shell return value ip_cmd='lsof-I tcp:'$port_be_kill echo "get the process command corresponding to the port number:" $ip_cmd echo "filter command string is:" $filter_content # execute command ip_status= `$ip_ cmd`

Note: the ip_status data type is a string

Convert 2-3 to an array

Since the string is not convenient for PID filtering, we need to split the above ip_status variable into an array with "spaces".

# separated by spaces, converted to an array variable array= (${ip_status// /})

2-4 traversal array, filter PID

First, iterate through the array above and extract each element

Then, filter out all data of type number and the previous value contains the filtered content

# determine the type of data function check () {local a = "$1" printf "% d"$a" & > / dev/null & & echo "integer" & & return printf "% d"$(echo $a | sed's / ^ [+ -]\? 0\ + / /')" & > / dev/null & & echo "integer" & return printf "% f"$a" & > / dev/null & echo "number "& & return [${# a}-eq 1] & & echo" char "& & return echo" string "}

Finally, use the kill-9 PID command to handle the corresponding process

# traversing the array for i in "${! array [@]}" Do # Note: there can be no spaces before and after the assignment equals sign item= "${array [I]}" # Note: filter the hexadecimal string # first convert it to a string, and then determine whether it starts with 0x # echo $item if [[$item! = 0x*]] then # non-hexadecimal data That is, PID if [$(check $item) = "integer"] then # determines whether the previous element contains a keyword # command line contains the keyword item_pre= "${array [I-1]}" # echo $item_pre # echo $filter_content if [[$item_] Pre = ~ $filter_content]] then # echo $item # call the kill-9 pid command to kill the process kill_cmd= "kill-9" $item echo $kill_cmd # execute the command Kill the process $kill_cmd fi done

2-5 set Alias

To run the Shell script with one click, we use Alias to set an alias for the command

Modify the ".bash _ profile" file to write the full path of the Shell script file and the execution command to a custom function

# vim .bash _ profile # alias defines kill_port () {cd / Users/xingag/Desktop/work. / kill_port_with_args.sh $1 $2} alias kp=kill_port

2-6 actual combat

Refresh the configuration file using the source .bash _ profile command to make the Alias configuration take effect immediately

If 8000 is occupied now, we just need to open the terminal and type the "kp 8000 python" command to kill the target process quickly.

The screenshot of the operation is as follows:

4. Last

It should be pointed out that the lsof command needs to be pre-installed under Linux. Take CentOS as an example.

# Centos install lsof yum install lsof

If it is Windows, the Shell script used by the processing port is different; it needs to be rewritten using the netstat/tasklist/taskkill command

In addition, Git Bash is recommended for executing Shell scripts on the PC side.

# Win processing port occupancy # 1, open cmd terminal cmd # 2, find the process occupied by the port and PID netstat-aon | findstr PORT # 3, query the process name according to PID | findstr PID # 4, use the taskkill command or shut down the process in the task manager. This is the article about "how to kill the port occupation with one click of the Shell script". I hope the above can be helpful to you. So that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report