learning java ProcessHandle 获取进程相当信息
Process p = rt.exec("notepad.exe"); ProcessHandle ph = p.toHandle(); System.out.println("进程是否运行:" + ph.isAlive()); System.out.println("进程ID:" + ph.pid()); System.out.println("父进程:" + ph.parent()); ProcessHandle.Info info = ph.info(); System.out.println("进程命令:" + info.command()); System.out.println("进程参数:" + info.arguments()); System.out.println("进程启动时间: " + info.startInstant()); System.out.println("进程积计运行时间:" + info.totalCpuDuration()); // 通过CompletetableFuture 在进程结束时运行某个命令 CompletableFuture<ProcessHandle> cf = ph.onExit(); cf.thenRunAsync(() -> System.out.println("程序退出")); Thread.sleep(5000);
output:
进程是否运行:true 进程ID:7004 父进程:Optional[13192] 进程命令:Optional[C:\Windows\System32\notepad.exe] 进程参数:Optional.empty 进程启动时间: Optional[2019-07-25T07:07:49.393Z] 进程积计运行时间:Optional[PT0.03125S] 程序退出