Skip to content

进程管理

进程查看

tasklist - 进程列表

cmd
tasklist                           # 显示所有进程
tasklist /v                        # 详细信息
tasklist /svc                      # 显示服务
tasklist /m                        # 显示模块
tasklist /fo table                 # 表格格式
tasklist /fo list                  # 列表格式
tasklist /fo csv                   # CSV 格式
tasklist /nh                       # 无表头

筛选进程

cmd
tasklist /fi "imagename eq notepad.exe"      # 按名称筛选
tasklist /fi "pid eq 1234"                   # 按 PID 筛选
tasklist /fi "memusage gt 100000"            # 内存大于100MB
tasklist /fi "status eq running"             # 运行中的进程
tasklist /fi "username eq administrator"     # 按用户筛选
tasklist /fi "windowtitle eq *notepad*"      # 按窗口标题
tasklist /fi "services eq dnscache"          # 按服务筛选

筛选运算符:

运算符说明
eq等于
ne不等于
gt大于
lt小于
ge大于等于
le小于等于

wmic - 进程信息

cmd
wmic process list brief           # 简要列表
wmic process list full            # 完整信息
wmic process get name,processid   # 指定字段
wmic process where "name='notepad.exe'" get processid,commandline
wmic process where "processid=1234" get name,executablepath
wmic process get name,workingsetsize /format:csv

PowerShell 进程查看

powershell
Get-Process                        # 所有进程
Get-Process -Name notepad          # 指定名称
Get-Process -Id 1234               # 指定 PID
Get-Process | Sort-Object CPU -Descending | Select-Object -First 10
Get-Process | Where-Object {$_.WorkingSet -gt 100MB}
Get-Process | Select-Object Name, Id, CPU, WorkingSet

进程终止

taskkill - 终止进程

cmd
taskkill /im notepad.exe          # 按名称终止
taskkill /pid 1234                # 按 PID 终止
taskkill /f /im notepad.exe       # 强制终止
taskkill /f /pid 1234             # 强制终止
taskkill /t /pid 1234             # 终止进程树
taskkill /f /t /pid 1234          # 强制终止进程树
taskkill /fi "memusage gt 100000" # 按条件终止
taskkill /fi "status eq not responding"  # 终止无响应进程

wmic 终止进程

cmd
wmic process where "name='notepad.exe'" delete
wmic process where "processid=1234" delete
wmic process where "commandline like '%script%'" delete

PowerShell 终止进程

powershell
Stop-Process -Name notepad        # 按名称终止
Stop-Process -Id 1234             # 按 PID 终止
Stop-Process -Name notepad -Force # 强制终止
Get-Process notepad | Stop-Process
Stop-Process -InputObject (Get-Process notepad)

进程启动

start - 启动程序

cmd
start notepad                     # 启动程序
start "" "C:\Program Files\app\app.exe"  # 带空格路径
start /max notepad                # 最大化启动
start /min notepad                # 最小化启动
start /wait setup.exe             # 等待完成
start /b app.exe                  # 后台启动
start /high app.exe               # 高优先级
start /low app.exe                # 低优先级
start /belownormal app.exe        # 低于正常优先级
start /abovenormal app.exe        # 高于正常优先级

PowerShell 启动进程

powershell
Start-Process notepad             # 启动程序
Start-Process "C:\app\app.exe"    # 指定路径
Start-Process notepad -ArgumentList "file.txt"
Start-Process notepad -WindowStyle Maximized
Start-Process notepad -WindowStyle Minimized
Start-Process notepad -Wait       # 等待完成
Start-Process notepad -Verb RunAs # 以管理员运行
Start-Process notepad -WorkingDirectory "C:\temp"

进程优先级

设置优先级

cmd
wmic process where "name='notepad.exe'" call setpriority 64
wmic process where "processid=1234" call setpriority 32
start /realtime app.exe           # 实时优先级
start /high app.exe               # 高优先级
start /abovenormal app.exe        # 高于正常
start /normal app.exe             # 正常优先级
start /belownormal app.exe        # 低于正常
start /low app.exe                # 低优先级

优先级级别:

级别说明
Realtime256实时
High128
Above Normal32768高于正常
Normal32正常
Below Normal16384低于正常
Low64

PowerShell 设置优先级

powershell
$process = Get-Process notepad
$process.PriorityClass = 'High'
$process.PriorityClass = 'AboveNormal'
$process.PriorityClass = 'Normal'
$process.PriorityClass = 'BelowNormal'
$process.PriorityClass = 'Low'

进程监控

tasklist 持续监控

cmd
:loop
cls
tasklist /fi "memusage gt 100000"
timeout /t 5 >nul
goto loop

PowerShell 监控

powershell
while ($true) {
    Clear-Host
    Get-Process | Sort-Object WorkingSet -Descending | Select-Object -First 10 Name, Id, @{N='Memory(MB)';E={[math]::Round($_.WorkingSet/1MB,2)}}
    Start-Sleep -Seconds 5
}

性能计数器

cmd
typeperf "\Processor(_Total)\% Processor Time"
typeperf "\Memory\Available MBytes"
typeperf "\Process(notepad)\% Processor Time"
typeperf "\Process(notepad)\Working Set"
typeperf -sc 5 "\Processor(_Total)\% Processor Time"  # 采样5次

服务管理

sc - 服务控制

cmd
sc query                          # 查询所有服务
sc query type= service            # 只查询服务
sc query servicename              # 查询指定服务
sc query state= all               # 查询所有状态
sc start servicename              # 启动服务
sc stop servicename               # 停止服务
sc pause servicename              # 暂停服务
sc continue servicename           # 继续服务
sc config servicename start= auto # 设置自动启动
sc config servicename start= demand # 设置手动启动
sc config servicename start= disabled # 禁用服务
sc delete servicename             # 删除服务
sc create newservice binPath= "C:\app\service.exe"
sc failure servicename reset= 86400 actions= restart/5000/restart/5000/restart/5000

net 服务命令

cmd
net start                         # 列出运行的服务
net start servicename             # 启动服务
net stop servicename              # 停止服务
net pause servicename             # 暂停服务
net continue servicename          # 继续服务

PowerShell 服务管理

powershell
Get-Service                       # 所有服务
Get-Service -Name *sql*           # 按名称筛选
Get-Service | Where-Object {$_.Status -eq 'Running'}
Start-Service -Name servicename
Stop-Service -Name servicename
Restart-Service -Name servicename
Set-Service -Name servicename -StartupType Automatic
Set-Service -Name servicename -StartupType Manual
Set-Service -Name servicename -StartupType Disabled
New-Service -Name "MyService" -BinaryPathName "C:\app\service.exe"
Remove-Service -Name servicename

进程调试

查看进程模块

cmd
tasklist /m /fi "imagename eq notepad.exe"
tasklist /m /fi "pid eq 1234"

查看进程句柄

cmd
handle.exe notepad.exe            # 需要 Sysinternals 工具
handle.exe -p 1234

查看进程网络连接

cmd
netstat -ano | findstr 1234
netstat -ano | findstr :80

查看进程打开的文件

cmd
openfiles /query /v
openfiles /query | findstr notepad

进程亲和性

设置 CPU 亲和性

cmd
wmic process where "name='notepad.exe'" call setaffinity 1
wmic process where "processid=1234" call setaffinity 3

亲和性值(十六进制):

  • CPU 0: 0x1
  • CPU 1: 0x2
  • CPU 0-1: 0x3
  • CPU 2: 0x4
  • CPU 0-2: 0x7

PowerShell 设置亲和性

powershell
$process = Get-Process notepad
$process.ProcessorAffinity = 3    # CPU 0 和 1

任务计划

schtasks - 任务计划

cmd
schtasks /query                   # 查询所有任务
schtasks /query /tn "TaskName"    # 查询指定任务
schtasks /query /fo list /v       # 详细列表
schtasks /create /tn "MyTask" /tr "C:\app\script.bat" /sc daily /st 09:00
schtasks /create /tn "MyTask" /tr "C:\app\script.bat" /sc weekly /d MON /st 09:00
schtasks /create /tn "MyTask" /tr "C:\app\script.bat" /sc monthly /d 1 /st 09:00
schtasks /create /tn "MyTask" /tr "C:\app\script.bat" /sc onstart
schtasks /create /tn "MyTask" /tr "C:\app\script.bat" /sc onlogon
schtasks /run /tn "MyTask"        # 运行任务
schtasks /end /tn "MyTask"        # 停止任务
schtasks /delete /tn "MyTask" /f  # 删除任务
schtasks /change /tn "MyTask" /tr "C:\app\newscript.bat"

计划类型:

类型说明
/sc minute每分钟
/sc hourly每小时
/sc daily每天
/sc weekly每周
/sc monthly每月
/sc onstart启动时
/sc onlogon登录时
/sc onidle空闲时

进程树

查看进程树

cmd
wmic process get parentprocessid,processid,name
tasklist /v /fo csv | findstr "PID"

PowerShell 进程树

powershell
Get-Process | Select-Object Name, Id, @{N='ParentId';E={(Get-WmiObject Win32_Process -Filter "ProcessId=$($_.Id)").ParentProcessId}}

实用示例

查找并终止进程

cmd
for /f "tokens=2" %a in ('tasklist /fi "imagename eq notepad.exe" /fo list ^| findstr "PID:"') do taskkill /pid %a /f

监控进程内存

cmd
:monitor
for /f "tokens=5" %a in ('tasklist /fi "imagename eq notepad.exe" /fo table /nh') do @echo Memory: %a KB
timeout /t 5 >nul
goto monitor

批量启动程序

cmd
start notepad
start calc
start mspaint

下一步学习