gpt4 book ai didi

linux - 如何仅在端口打开时终止在端口上运行的进程?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:50:48 26 4
gpt4 key购买 nike

目前,我可以通过以下方式终止在端口上运行的进程:

//other scripts that should be executed
...........................
sudo kill $( sudo lsof -i:9005 -t )
...........................
//other scripts that should be executed

现在,我想首先测试端口 9005 是否真的打开,然后才尝试杀死它。如果端口没有打开,我不想执行 kill 脚本。我想确保无论端口是打开还是关闭,后续脚本都会执行。所以,我正在寻找类似的东西:

//other scripts that should be executed
.............
<test-if-port-9005-is-open> && sudo kill $( sudo lsof -i:9005 -t )
.............
// other scripts that should be executed

我怎样才能做到这一点?

最佳答案

你可以这样做:

nc -zv 127.0.0.1 9005 && sudo kill $( sudo lsof -i:9005 -t )

如果 tcp 连接在提供的端口上成功,它会终止进程。然后它继续前进。在 macOS High Sierra 上测试过。

关于linux - 如何仅在端口打开时终止在端口上运行的进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47467248/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com