Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mac.osbot;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.List;
- public class ProcessManager {
- public static boolean isProcessRunning(int pid) throws InterruptedException {
- if (System.getProperty("os.name").startsWith("Windows")) {
- return isProcessIdRunningOnWindows(pid);
- } else {
- return isProcessIdRunningOnLinux(pid);
- }
- }
- /**
- * Queries {@code tasklist} if the process ID {@code pid} is running.
- *
- * @param pid the PID to check
- * @return {@code true} if the PID is running, {@code false} otherwise
- * @throws InterruptedException
- */
- public static boolean isProcessIdRunningOnWindows(int pid) throws InterruptedException {
- /*try {
- Runtime runtime = Runtime.getRuntime();
- String cmds[] = { "cmd", "/c", "tasklist /FI \"PID eq " + pid + "\"" };
- Process proc = runtime.exec(cmds);
- InputStream inputstream = proc.getInputStream();
- InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
- BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
- String line;
- while ((line = bufferedreader.readLine()) != null) {
- // Search the PID matched lines single line for the sequence: " 1300 "
- // if you find it, then the PID is still running.
- if (line.contains(" " + pid + " ")) {
- return true;
- }
- }
- return false;
- } catch (Exception ex) {
- ex.printStackTrace();
- System.out.println("Cannot query the tasklist for some reason.");
- System.exit(0);
- }
- */
- return getJavaPIDs().contains(pid);
- }
- public static boolean isProcessIdRunningOnLinux(int pid) throws InterruptedException {
- return getJavaPIDs().contains(pid);
- }
- public static List<Integer> getJavaPIDs() throws InterruptedException {
- if (System.getProperty("os.name").startsWith("Windows")) {
- return getJavaPIDsWindows();
- } else {
- return getJavaPIDsLinux();
- }
- }
- public static List<Integer> getJavaPIDsWindows() throws InterruptedException {
- System.out.println("getting windows pids");
- List<Integer> pids = new ArrayList<>();
- try {
- Process process = Runtime.getRuntime().exec("tasklist /FI \"IMAGENAME eq java.exe\" /NH");
- try (final InputStream stdout = process.getInputStream();
- final InputStreamReader inputStreamReader = new InputStreamReader(stdout);
- final BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
- String processInfo;
- while ((processInfo = bufferedReader.readLine()) != null) {
- processInfo = processInfo.trim();
- String[] values = processInfo.split("\\s+");
- if (values.length > 1 && values[1].equals("No"))
- return pids;
- if (values.length >= 2) {
- pids.add(Integer.parseInt(values[1]));
- }
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return pids;
- }
- public static List<Integer> getJavaPIDsLinux() {
- List<Integer> pids = new ArrayList<>();
- try {
- Process process = Runtime.getRuntime().exec("jps -m");
- try (final InputStream stdout = process.getInputStream();
- final InputStreamReader inputStreamReader = new InputStreamReader(stdout);
- final BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
- String processInfo;
- while ((processInfo = bufferedReader.readLine()) != null) {
- if (!processInfo.isEmpty() && processInfo.contains("BotApplication")) {
- String[] data = processInfo.split(" ");
- try {
- int pid = Integer.parseInt(data[0]);
- pids.add(pid);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /*
- * processInfo = processInfo.trim(); System.out.println("Trimmed " +
- * processInfo); String[] values = processInfo.split("\\s+"); if (values.length
- * > 0) { System.out.println("Values > 0 " + values[0] );
- * pids.add(Integer.parseInt(values[0])); }
- */
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return pids;
- }
- public static void killProcess(final int PID) {
- try {
- if (System.getProperty("os.name").startsWith("Windows")) {
- Runtime.getRuntime().exec("Taskkill /PID " + PID + " /F");
- } else {
- Runtime.getRuntime().exec("kill -9 " + PID);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement