- 批处理脚本教程
- 批处理脚本 - 首页
- 批处理脚本 - 概述
- 批处理脚本 - 环境
- 批处理脚本 - 命令
- 批处理脚本 - 文件
- 批处理脚本 - 语法
- 批处理脚本 - 变量
- 批处理脚本 - 注释
- 批处理脚本 - 字符串
- 批处理脚本 - 数组
- 批处理脚本 - 决策
- 批处理脚本 - 运算符
- 批处理脚本 - 日期与时间
- 批处理脚本 - 输入/输出
- 批处理脚本 - 返回代码
- 批处理脚本 - 函数
- 批处理脚本 - 进程
- 批处理脚本 - 别名
- 批处理脚本 - 设备
- 批处理脚本 - 注册表
- 批处理脚本 - 网络
- 批处理脚本 - 打印
- 批处理脚本 - 调试
- 批处理脚本 - 日志记录
- 批处理脚本资源
- 批处理脚本 - 快速指南
- 批处理脚本 - 有用资源
- 批处理脚本 - 讨论
批处理脚本 - 进程
本章将讨论批处理脚本中涉及的各种进程。
查看正在运行的进程列表
在批处理脚本中,可以使用 TASKLIST 命令获取系统中当前正在运行的进程列表。
语法
TASKLIST [/S system [/U username [/P [password]]]] [/M [module] | /SVC | /V] [/FI filter] [/FO format] [/NH]
| 序号 | 选项与说明 |
|---|---|
| 1. |
/S system 指定要连接到的远程系统 |
| 2. |
/U [domain\]user 指定命令应在其下执行的用户上下文。 |
| 3. |
/P [password] 指定给定用户上下文的密码。如果省略,则提示输入。 |
| 4. |
/M [module] 列出当前正在使用给定 exe/dll 名称的所有任务。如果未指定模块名称,则显示所有已加载的模块。 |
| 5. |
/SVC 显示每个进程中托管的服务。 |
| 6. |
/V 显示详细的任务信息。 |
| 7. |
/FI filter 显示与筛选器指定的给定条件匹配的一组任务。 |
| 8. |
/FO format 指定输出格式。有效值:“TABLE”、“LIST”、“CSV”。 |
| 9. |
/NH 指定输出中不应显示“列标题”。仅对“TABLE”和“CSV”格式有效。 |
示例
TASKLIST
以上命令将获取本地系统上所有正在运行的进程的列表。以下是运行以上命令时呈现的输出快照。从以下输出可以看到,您不仅可以获得系统上正在运行的各种进程,还可以获得每个进程的内存使用情况。
Image Name PID Session Name Session# Mem Usage ========================= ======== ================ =========== ============ System Idle Process 0 Services 0 4 K System 4 Services 0 272 K smss.exe 344 Services 0 1,040 K csrss.exe 528 Services 0 3,892 K csrss.exe 612 Console 1 41,788 K wininit.exe 620 Services 0 3,528 K winlogon.exe 648 Console 1 5,884 K services.exe 712 Services 0 6,224 K lsass.exe 720 Services 0 9,712 K svchost.exe 788 Services 0 10,048 K svchost.exe 832 Services 0 7,696 K dwm.exe 916 Console 1 117,440 K nvvsvc.exe 932 Services 0 6,692 K nvxdsync.exe 968 Console 1 16,328 K nvvsvc.exe 976 Console 1 12,756 K svchost.exe 1012 Services 0 21,648 K svchost.exe 236 Services 0 33,864 K svchost.exe 480 Services 0 11,152 K svchost.exe 1028 Services 0 11,104 K svchost.exe 1048 Services 0 16,108 K wlanext.exe 1220 Services 0 12,560 K conhost.exe 1228 Services 0 2,588 K svchost.exe 1276 Services 0 13,888 K svchost.exe 1420 Services 0 13,488 K spoolsv.exe 1556 Services 0 9,340 K
tasklist > process.txt
以上命令获取 tasklist 显示的输出并将其保存到 process.txt 文件中。
tasklist /fi "memusage gt 40000"
以上命令只获取内存大于 40MB 的进程。以下是一个可以呈现的示例输出。
Image Name PID Session Name Session# Mem Usage ========================= ======== ================ =========== ============ dwm.exe 916 Console 1 127,912 K explorer.exe 2904 Console 1 125,868 K ServerManager.exe 1836 Console 1 59,796 K WINWORD.EXE 2456 Console 1 144,504 K chrome.exe 4892 Console 1 123,232 K chrome.exe 4976 Console 1 69,412 K chrome.exe 1724 Console 1 76,416 K chrome.exe 3992 Console 1 56,156 K chrome.exe 1168 Console 1 233,628 K chrome.exe 816 Console 1 66,808 K
终止特定进程
允许运行 Microsoft Windows XP Professional、Windows 2003 或更高版本的使用者通过进程 ID (PID) 或图像名称从 Windows 命令行终止任务。用于此目的的命令是 TASKKILL 命令。
语法
TASKKILL [/S system [/U username [/P [password]]]] { [/FI filter]
[/PID processid | /IM imagename] } [/T] [/F]
| 序号 | 选项与说明 |
|---|---|
| 1. |
/S system 指定要连接到的远程系统 |
| 2. |
/U [domain\]user 指定命令应在其下执行的用户上下文。 |
| 3. |
/P [password] 指定给定用户上下文的密码。如果省略,则提示输入。 |
| 4. |
/FI FilterName 应用筛选器以选择一组任务。允许使用“*”。例如 imagename eq acme* 参见下面的筛选器以获取其他信息和示例。 |
| 5. |
/PID processID 指定要终止的进程的 PID。使用 TaskList 获取 PID。 |
| 6. |
/IM ImageName 指定要终止的进程的图像名称。通配符“*”可用于指定所有任务或图像名称。 |
| 7. |
/T 终止指定的进程及其由其启动的任何子进程。 |
| 8. |
/F 指定强制终止进程。 |
示例
taskkill /f /im notepad.exe
以上命令将终止打开的记事本任务(如果已打开)。
taskill /pid 9214
以上命令将终止进程 ID 为 9214 的进程。
启动新进程
DOS 脚本也可以启动一个全新的进程。这是通过使用 START 命令实现的。
语法
START "title" [/D path] [options] "command" [parameters]
其中
title − CMD 窗口标题栏的文本(必需)。
path − 启动目录。
command − 要运行的命令、批处理文件或可执行程序。
parameters − 传递给命令的参数。
| 序号 | 选项与说明 |
|---|---|
| 1. |
/MIN 最小化启动窗口 |
| 2. |
/MAX 最大化启动窗口。 |
| 3. |
/LOW 使用 IDLE 优先级类。 |
| 4. |
/NORMAL 使用 NORMAL 优先级类。 |
| 5. |
/ABOVENORMAL 使用 ABOVENORMAL 优先级类。 |
| 6. |
/BELOWNORMAL 使用 BELOWNORMAL 优先级类。 |
| 7. |
/HIGH 使用 HIGH 优先级类。 |
| 8. |
/REALTIME 使用 REALTIME 优先级类。 |
示例
START "Test Batch Script" /Min test.bat
以上命令将在新窗口中运行批处理脚本 test.bat。窗口将以最小化模式启动,并且标题为“测试批处理脚本”。
START "" "C:\Program Files\Microsoft Office\Winword.exe" "D:\test\TESTA.txt"
以上命令实际上将在另一个进程中运行 Microsoft Word,然后在 MS Word 中打开文件 TESTA.txt。