我有一个 react-native 应用程序,我想为启动测试版本编写一个脚本。如果它还活着,我需要击落 :8081 端口。命令:
lsof -i :8081
kill -9 <PID>
得到这个结果的 lsof :
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
chrome 2423 loow 127u IPv4 13749099 0t0 TCP localhost.localdomain:36650->localhost.localdomain:tproxy (ESTABLISHED)
qemu-syst 15091 loow 64u IPv4 13795587 0t0 TCP localhost.localdomain:43518->localhost.localdomain:tproxy (ESTABLISHED)
qemu-syst 15091 loow 66u IPv4 13795588 0t0 TCP localhost.localdomain:43520->localhost.localdomain:tproxy (ESTABLISHED)
qemu-syst 15091 loow 89u IPv4 13777485 0t0 TCP localhost.localdomain:40500->localhost.localdomain:tproxy (ESTABLISHED)
node 16210 loow 16u IPv6 13747716 0t0 TCP *:tproxy (LISTEN)
node 16210 loow 18u IPv6 13751322 0t0 TCP localhost.localdomain:tproxy->localhost.localdomain:36650 (ESTABLISHED)
node 16210 loow 19u IPv6 13798473 0t0 TCP localhost.localdomain:tproxy->localhost.localdomain:43518 (ESTABLISHED)
node 16210 loow 21u IPv6 13798475 0t0 TCP localhost.localdomain:tproxy->localhost.localdomain:43520 (ESTABLISHED)
node 16210 loow 27u IPv6 13777958 0t0 TCP localhost.localdomain:tproxy->localhost.localdomain:40500 (ESTABLISHED)
在这种情况下,我需要的是节点服务的 pid 16210。如何从 lsof 获取它?
我lsof -ti :8081
尝试了仅返回pid,但它返回了所有这些。我试图用 grep: 扩展它lsof -ti :8081 | grep node
,它什么都不返回。
所以我只需要进程 id 就可以将其放入kill -9 $PID
.