Index / Shell

通过端口号关闭当前进程

原文:https://ichochy.com/posts/shell/20190412.html

使用Shell脚本的方式,通过端口号来关闭当前进程

查看脚本

lsof  -i TCP:9100 | grep LISTEN | awk '{print $2}'

使用到三个命令lsofgrepawk

lsof 获取端口进程列表

grep 精准匹配进程

awk 获取进程号

完整示例:

#!/bin/bash

PROCESS=echo | lsof  -i TCP:9100 | grep LISTEN | awk '{print $2}'
if [ $PROCESS ]; then
kill -9 $PROCESS
echo kill $PROCESS
fi