99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁(yè)大數(shù)據(jù)時(shí)代該死的端口占用!教你用 Shell 腳本一鍵干掉它
該死的端口占用!教你用 Shell 腳本一鍵干掉它
2021-03-02
收藏

來(lái)源:AirPython

作者:星安果

該死的端口占用!教你用 Shell 腳本一鍵干掉它

1. 前言

大家好,我是安果!

在 Web 開發(fā)中,經(jīng)常會(huì)遇到「端口被占用」的場(chǎng)景

常規(guī)解決方案是:

  • 使用 lsof -i 命令查詢占用端口的進(jìn)程 PID
  • 利用 kill -9 PID 干掉目標(biāo)進(jìn)程

雖然只有 2 步,我也覺(jué)得很繁瑣,即:既要記住命令,又要輸入兩次

本篇文章將編寫 Shell 腳本,來(lái)實(shí)現(xiàn)一鍵干掉端口占用

2. 實(shí)現(xiàn)

相比 Python,Shell 腳本常用于處理偏操作系統(tǒng)底層的業(yè)務(wù),簡(jiǎn)單、開發(fā)效率高

以 Mac OSX 為例,在本地創(chuàng)建一個(gè) .sh 文件,實(shí)現(xiàn)步驟如下:

2-1 定義端口號(hào)及過(guò)濾內(nèi)容參數(shù)

將要處理的端口號(hào)及命令行過(guò)濾內(nèi)容參數(shù)化,然后在 Shell 腳本中賦值給兩個(gè)變量

  • $1執(zhí)行腳本的第一個(gè)參數(shù),即:端口號(hào)
  • $2執(zhí)行腳本的第二個(gè)參數(shù),即:過(guò)濾進(jìn)程命令內(nèi)容

# 要干掉的端口號(hào)
port_be_kill=$1
# 過(guò)濾內(nèi)容
filter_content=$2

2-2 獲取端口占用進(jìn)程返回值

使用端口號(hào)組成 lsof -i 命令,執(zhí)行這條命令,將返回值賦值給變量 ip_status

# 獲取Shell返回值
ip_cmd='lsof -i tcp:'$port_be_kill
echo "獲取端口號(hào)對(duì)應(yīng)的進(jìn)程命令:"$ip_cmd
echo "過(guò)濾命令字符串為:"$filter_content
# 執(zhí)行命令
ip_status=`$ip_cmd`

注意:ip_status 數(shù)據(jù)類型為字符串

2-3 轉(zhuǎn)為數(shù)組

由于字符串不便于 PID 過(guò)濾,我們需要將上面的 ip_status 變量按「空格」分割成一個(gè)數(shù)組

# 以空格來(lái)分隔,轉(zhuǎn)為一個(gè)數(shù)組變量
array=(${ip_status// / })

2-4 遍歷數(shù)組,過(guò)濾 PID

首先,遍歷上面的數(shù)組,提取每一個(gè)元素

然后,過(guò)濾出所有類型為 number、并且上一個(gè)值包含過(guò)濾內(nèi)容的數(shù)據(jù)

# 判斷數(shù)據(jù)的類型
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"
}

最后,使用 kill -9 PID 命令處理對(duì)應(yīng)的進(jìn)程

# 遍歷數(shù)組
for i in "${!array[@]}"; do
# 注意:賦值等號(hào)=前后不能有空格
item="${array[i]}"
# 注意:過(guò)濾十六進(jìn)制字符串
# 先轉(zhuǎn)為字符串,然后判斷是否以0x開頭
# echo $item
if [[ $item != 0x* ]]
then
# 非十六進(jìn)制數(shù)據(jù),即:PID
if [ $(check $item) = "integer" ]
then
# 判斷上一個(gè)元素是否包含關(guān)鍵字
# 命令行是否包含關(guān)鍵字
item_pre="${array[i-1]}"
# echo $item_pre
# echo $filter_content
if [[ $item_pre =~ $filter_content ]]
then
# echo $item
# 調(diào)用kill-9 pid命令干掉進(jìn)程
kill_cmd="kill -9 "$item
echo $kill_cmd
# 執(zhí)行命令,干掉進(jìn)程
$kill_cmd
fi
fi
fi
done

2-5 設(shè)置 Alias

為了一鍵運(yùn)行 Shell 腳本,我們使用 Alias 給命令設(shè)置一個(gè)別名

修改「.bash_profile」文件,將 Shell 腳本文件的完整路徑及執(zhí)行命令寫入到一個(gè)自定義的函數(shù)中

# vim .bash_profile
# alias定義
kill_port() {
cd /Users/xingag/Desktop/work
./kill_port_with_args.sh $1 $2
}
alias kp=kill_port

2-6 實(shí)戰(zhàn)一下

使用 source .bash_profile 命令刷新配置文件,讓 Alias 配置立即生效

假如現(xiàn)在 8000 被占用,我們只需要打開終端輸入「kp 8000 python」命令即可以快速干掉目標(biāo)進(jìn)程

運(yùn)行截圖如下:

該死的端口占用!教你用 Shell 腳本一鍵干掉它

3. 最后

需要指出的是,Linux 下需要預(yù)先安裝 lsof 命令,以 CentOS 為例

# Centos安裝lsof
yum install lsof

如果是 Windows,處理端口占用的 Shell 腳本不一樣;它需要使用 netstat/tasklist/taskkill 命令去改寫

另外,PC 端執(zhí)行 Shell 腳本建議使用 Git Bash

# Win處理端口占用
# 1、打開cmd終端
cmd
# 2、查找端口占用的進(jìn)程及PID
netstat -aon|findstr PORT
# 3、根據(jù)PID查詢進(jìn)程名稱
tasklist|findstr PID
# 4、使用taskkill命令或在任務(wù)管理器中關(guān)掉進(jìn)程

如果你覺(jué)得文章還不錯(cuò),請(qǐng)大家 點(diǎn)贊、分享、留言 下,因?yàn)檫@將是我持續(xù)輸出更多優(yōu)質(zhì)文章的最強(qiáng)動(dòng)力!

數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說(shuō)明請(qǐng)參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }