go - close a port from go code - not cmd to lsof
问题描述
Env
os: MacOS
go version: go1.14.3 darwin/amd64
I have a grpc server running and it does not always close the port (out of my control).
Do you know if it is possible to close a port from code?
It looks like you can do - but I need the pid?
err := syscall.Kill(<PID>, syscall.SYS_KILL)
if err == nil {
fmt.Println("success")
}
I use to have this working in Powershell, but would like to move it to Go:
# lsof -ti tcp:$Port | xargs kill
Write-Host "Before: lsof -i :$Port"
lsof -i :$Port
$OutputVariable = (lsof -i :$Port | awk 'NR==2 { print $2 }') | Out-String
Write-Host "Process killed: $OutputVariable"
Stop-Process -Id $OutputVariable
Write-Host "After: lsof -i :$Port"
lsof -i :$Port
解决方案
推荐阅读
- python - 并排修改 2 个列表中的列表
- c++ - C ++中的二进制搜索分段错误
- python - TypeError:find_elements_by_tag_name() 采用 2 个位置参数,但使用 Selenium 和 Python 使用 find_elements_by_tag_name() 给出了 3 个错误
- python - 回调导致 ValueError
- c# - Azure Pipeline - 使用不同的凭据打开 RemoteWebDriver
- android - “IllegalArgumentException:图层被重绘”,同时使用 collectAsLazyListItems() 和 JetpackCompose 分页滚动太快
- c++ - 排序和跟踪结构中的索引
- youtube-data-api - 如何在开发模式下使用 Youtube API Key?
- php - WordPress多语言无插件
- sql - 在 Ruby on Rails / Postgresql 中查找多个连续日期(日期时间)