目前,我可以通过以下方式终止在端口上运行的进程:
//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/