Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.35 KB | None | 0 0
  1. package mac.osbot;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. public class ProcessManager {
  11.  
  12.     public static boolean isProcessRunning(int pid) throws InterruptedException {
  13.         if (System.getProperty("os.name").startsWith("Windows")) {
  14.             return isProcessIdRunningOnWindows(pid);
  15.         } else {
  16.             return isProcessIdRunningOnLinux(pid);
  17.         }
  18.     }
  19.    
  20.  
  21.     /**
  22.      * Queries {@code tasklist} if the process ID {@code pid} is running.
  23.      *
  24.      * @param pid the PID to check
  25.      * @return {@code true} if the PID is running, {@code false} otherwise
  26.      * @throws InterruptedException
  27.      */
  28.     public static boolean isProcessIdRunningOnWindows(int pid) throws InterruptedException {
  29.         /*try {
  30.             Runtime runtime = Runtime.getRuntime();
  31.             String cmds[] = { "cmd", "/c", "tasklist /FI \"PID eq " + pid + "\"" };
  32.             Process proc = runtime.exec(cmds);
  33.  
  34.             InputStream inputstream = proc.getInputStream();
  35.             InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
  36.             BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
  37.             String line;
  38.             while ((line = bufferedreader.readLine()) != null) {
  39.                 // Search the PID matched lines single line for the sequence: " 1300 "
  40.                 // if you find it, then the PID is still running.
  41.                 if (line.contains(" " + pid + " ")) {
  42.                     return true;
  43.                 }
  44.             }
  45.  
  46.             return false;
  47.         } catch (Exception ex) {
  48.             ex.printStackTrace();
  49.             System.out.println("Cannot query the tasklist for some reason.");
  50.             System.exit(0);
  51.         }
  52. */
  53.         return getJavaPIDs().contains(pid);
  54.     }
  55.  
  56.     public static boolean isProcessIdRunningOnLinux(int pid) throws InterruptedException {
  57.         return getJavaPIDs().contains(pid);
  58.     }
  59.  
  60.     public static List<Integer> getJavaPIDs() throws InterruptedException {
  61.         if (System.getProperty("os.name").startsWith("Windows")) {
  62.             return getJavaPIDsWindows();
  63.         } else {
  64.             return getJavaPIDsLinux();
  65.         }
  66.     }
  67.  
  68.     public static List<Integer> getJavaPIDsWindows() throws InterruptedException {
  69.         System.out.println("getting windows pids");
  70.         List<Integer> pids = new ArrayList<>();
  71.         try {
  72.             Process process = Runtime.getRuntime().exec("tasklist /FI \"IMAGENAME eq java.exe\" /NH");
  73.             try (final InputStream stdout = process.getInputStream();
  74.                     final InputStreamReader inputStreamReader = new InputStreamReader(stdout);
  75.                     final BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
  76.                 String processInfo;
  77.                 while ((processInfo = bufferedReader.readLine()) != null) {
  78.                     processInfo = processInfo.trim();
  79.                     String[] values = processInfo.split("\\s+");
  80.                     if (values.length > 1 && values[1].equals("No"))
  81.                         return pids;
  82.                     if (values.length >= 2) {
  83.                         pids.add(Integer.parseInt(values[1]));
  84.                     }
  85.                 }
  86.             }
  87.         } catch (IOException e) {
  88.             e.printStackTrace();
  89.         }
  90.         return pids;
  91.     }
  92.  
  93.     public static List<Integer> getJavaPIDsLinux() {
  94.         List<Integer> pids = new ArrayList<>();
  95.         try {
  96.             Process process = Runtime.getRuntime().exec("jps -m");
  97.             try (final InputStream stdout = process.getInputStream();
  98.                     final InputStreamReader inputStreamReader = new InputStreamReader(stdout);
  99.                     final BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
  100.                 String processInfo;
  101.                 while ((processInfo = bufferedReader.readLine()) != null) {
  102.                     if (!processInfo.isEmpty() && processInfo.contains("BotApplication")) {
  103.                         String[] data = processInfo.split(" ");
  104.                         try {
  105.                             int pid = Integer.parseInt(data[0]);
  106.                             pids.add(pid);
  107.                         } catch (Exception e) {
  108.                             e.printStackTrace();
  109.                         }
  110.                     }
  111.                     /*
  112.                      * processInfo = processInfo.trim(); System.out.println("Trimmed " +
  113.                      * processInfo); String[] values = processInfo.split("\\s+"); if (values.length
  114.                      * > 0) { System.out.println("Values > 0 " + values[0] );
  115.                      * pids.add(Integer.parseInt(values[0])); }
  116.                      */
  117.                 }
  118.             }
  119.         } catch (IOException e) {
  120.             e.printStackTrace();
  121.         }
  122.         return pids;
  123.     }
  124.  
  125.     public static void killProcess(final int PID) {
  126.         try {
  127.             if (System.getProperty("os.name").startsWith("Windows")) {
  128.                 Runtime.getRuntime().exec("Taskkill /PID " + PID + " /F");
  129.             } else {
  130.                 Runtime.getRuntime().exec("kill -9 " + PID);
  131.             }
  132.         } catch (IOException e) {
  133.             e.printStackTrace();
  134.         }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement