常用cmd命令
tasklist //查询全部进程
taskkill /pid 313592 -f //杀死一个进程一个进程
tasklist|find /i "explorer.exe" //根据进程名称判断是否存在
tasklist|findstr 3120 //根据pid判断进程是否存在
D:/BaiduYunDownload/WinRAR/WinRAR.exe a d:/abc.rar d:/test.txt //压缩text.txt文件
应用:找到占用了端口的进程将其杀掉
//查看端口占用情况
netstat -aon|findstr "8080"
//查看是哪个进程占用
tasklist|findstr "your_pid"
//Ctrl+shift+Ecs 详细信息找出相应进程名和pid将其杀掉
windows下使用shell
安装完成后输入以下命令查看home所在的目录。
cygpath -dm "`pwd`"
输出:C:/cygwin64/home
思考
如何通过java代码来执行shell脚本呢
public static boolean isWindows() {
return System.getProperty("os.name").startsWith("Windows");
}
public static Long executeShell(String commandStr) throws Exception {
Long pid = null;
String[] commandArr = new String[]{"C:/cygwin64/bin/sh", "-c", commandStr};
//String[] commandArr = new String[]{"sh", "-c", commandStr};
//if (isWindows()) {
// commandStr = "tasklist|findstr " + pid;
// commandArr = new String[]{"cmd.exe", "/C", commandStr};
//}
Process process = Runtime.getRuntime().exec(commandArr);
if (process != null) {
//获取进程id
Field f = process.getClass().getDeclaredField("handle");
f.setAccessible(true);
long handl = f.getLong(process);
Kernel32 kernel = Kernel32.INSTANCE;
WinNT.HANDLE handle = new WinNT.HANDLE();
handle.setPointer(Pointer.createConstant(handl));
pid = Long.valueOf(kernel.GetProcessId(handle));
process.waitFor();
// process.destroy();
}
return pid;
}
以上代码可以在直接将要执行的命令作为参数传入