package com.cloudwise.doop.pg.util;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class PsCommandParser {
public static void main(String[] args) {
String psOutput =
"UID PID PPID C STIME TTY TIME CMD\n" +
"root 1 0 0 Sep 06 - 18:23 /etc/init\n" +
"root 4456670 1 0 Sep 06 - 0:41 /usr/ccs/bin/shlap64\n" +
"root 4522018 6094890 0 Sep 06 - 0:00 /usr/sbin/rpc.lockd -d 0\n" +
"root 5046284 1 0 Sep 06 - 0:00 /usr/lib/errdemon\n" +
"111 5177472 1 0 18:56:11 - 0:00 ksh -c /home/xxx.ksh >/dev/null 2>&1 </dev/null &\n" +
"root 5308600 1 0 Sep 06 - 1075:50 /usr/sbin/syncd 60\n" +
"111 5701652 45941800 0 18:56:13 - 0:00 /usr/bin/perl -w xxx.pl 0x11002125 -log xxx.log -max 10000\n" +
"root 5898300 6094890 0 Sep 06 - 0:00 /usr/sbin/biod 6\n";
Map<String, String> processMap = new HashMap<>();
Scanner scanner = new Scanner(psOutput);
scanner.nextLine(); // Skip header
while (scanner.hasNextLine()) {
String line = scanner.nextLine().trim();
String[] parts = line.split("\\s+");
String pid = parts[1];
int cmdIndex = 7; // Typically, CMD starts at index 7
if (parts[6].equals("-")) {
cmdIndex = 8; // Adjust if TIME is missing and represented as '-'
}
StringBuilder cmdBuilder = new StringBuilder();
for (int i = cmdIndex; i < parts.length; i++) {
cmdBuilder.append(parts[i]).append(" ");
}
String cmd = cmdBuilder.toString().trim();
processMap.put(pid, cmd);
}
scanner.close();
// Print the map to verify
processMap.forEach((key, value) -> System.out.println("PID: " + key + ", CMD: " + value));
}
}
PsCommandParser
于 2024-06-20 01:20:07 首次发布