JZTXT
  • 首页
  • Ai
  • Java
  • Python
  • Android
  • Mysql
  • JavaScript
  • Html
  • CSS

Linux Shell 判断指定端口是否已经被占用

发布时间 2023-07-29 20:59:50作者: 白马黑衣

一、方法

1. 使用命令

netstat [-acCeFghilMnNoprstuvVwx][-A<网络类型>][--ip]

2. 脚本

(1) 创建

vi test.sh

(2) 初始化

if netstat -an | grep "$1" | grep -i listen >/dev/null ; then
    echo "$1 is running."
else
    echo "$1 is not running."
fi

(3) 赋权

chmod +x test.sh

(4) 运行

./test.sh 8080

二、参考

1. 官方

https://linux.die.net/man/8/netstat

https://www.runoob.com/linux/linux-comm-netstat.html

2. 其他

https://www.cyberciti.biz/faq/unix-linux-check-if-port-is-in-use-command/

https://unix.stackexchange.com/questions/149419/how-to-check-whether-a-particular-port-is-open-on-a-machine-from-a-shell-script

https://serverfault.com/questions/1078483/how-to-find-out-what-service-is-listening-on-a-specific-port-of-a-ubuntu-server/1079542

https://serverfault.com/questions/309052/check-if-port-is-open-or-closed-on-a-linux-server

    本栏目推荐文章
  • spark 3.x idea linux远程开发环境搭建
  • linux环境htop安装
  • 【Shell基础】Bash基础与Linux三剑客
  • OpenHarmony 上跑CV 应用 - Windows上搭建 Linux 桌面系统
  • linux 安装最新版本git
  • linux环境Python安装
  • 纪念一下,在国产麒麟linux下跑dotnet8,运行起来了
  • linux 磁盘清理
  • Linux的守护进程 [补档-2023-08-10]
  • Linux的信号管理 [补档-2023-07-30]
版权声明:本网站为非赢利性站点,本网站所有内容均来源于互联网相关站点自动搜索采集信息,相关链接已经注明来源。
联系我们