Linux / Windows 中 进程 的 查看 和杀死
Linux
# 查看进程
ps -aux
# 管道命令
# 查看内某个软件或者端口的进程
ps -aux | grep 软件名/端口
# 例如:
ps -aux | grep tomcat
ps -aux | grep 8080
# 杀死进程
kill -9 PID
kill -9 进程号
# 通杀
pkill -9 PID
Windows
netstat -aon|findstr 端口号
# 查看进程
tasklist|findstr 进程号
# 杀死进程
taskkill /pid 进程号 /f
感觉这样太麻烦, 编写一个 powershell 脚本
# 设置执行策略 Set-ExecutionPolicy RemoteSigned
# 注释作用
@'
用户输入 n 个端口号, 并杀死这些端口号的进程
'@
For ($i=0; $i -lt $args.Count; $i++)
{
# 输出信息内容
Write-Host "端口号 $i : $($args[$i])"
# 查看端口号进程
$netstat = netstat -aon | findstr $args[$i]
# 获取进程值
$process = -Split $netstat
Write-Host "进程号 $i : $($process[-1])"
# 查看进程
tasklist | findstr $process[-1]
# 杀死进程
taskkill /pid $process[-1] /f
}
实例:
.\skill_port.ps1 8080 8081 8082