1.查看所有进程的
wmic process get caption,commandline /value
2.列举当前的进程、进程路径、命令行、进程ID、父进程ID、线程数,内存使用
wmic process get name,executablepath,commandline,processid,parentprocessid,threadcount,workingsetsize/value
3. 根据执行的文件,如python.exe 的所属的进程
wmic process where caption="python.exe" get processid,caption,commandline /value
输出
C:\Users\aigoes\Desktop\detection\result>wmic process where caption="python.exe" get processid,caption,commandline /value
Caption=python.exe #执行文件
CommandLine=python detection.py #运行的命令
ProcessId=17956 #pid
Caption=python.exe
CommandLine=E:\install\anaconda2.5\python.exe C:/Users/aigoes/Desktop/detection/tools/test.py
ProcessId=2304
4.根据cmd命令查询进程,如查询命令为python detection.py的进程
wmic process where CommandLine="python detection.py" get processid /value
输出
ProcessId=17956
5.杀死进程
taskkill /F /PID 17956
输出
成功: 已终止 PID 为 17956 的进程。
6. java 连续调用cmd命令
如在命令1进入的目录下,跳转到目录2.使用&&来连接命令
public static void run_test(){
Process process;
String cmd="cmd /c \"c: && cd \\Users\\aigoes\\Desktop\\detection\\tools && python test.py\"";
try {
Runtime runtime = Runtime.getRuntime();
System.out.println("开始执行。。。");
process = runtime.exec(cmd);
System.out.println(cmd);
process.waitFor();
//打印执行的输出结果
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "gbk"); //gbk:解决输出乱码
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null){ //注意,当python test.py进程执行结束才会执行输出
System.out.println(line);
}
is.close();
isr.close();
br.close();
}catch (Exception e){
e.printStackTrace();
}
}