Android Shell Run findProcessId(String command)

Here you can find the source of findProcessId(String command)

Description

find Process Id

License

Open Source License

Declaration

public static int findProcessId(String command) 

Method Source Code

//package com.java2s;
/* See LICENSE for licensing information */

import java.io.BufferedReader;
import java.io.File;

import java.io.InputStreamReader;

import java.net.URLEncoder;
import java.util.StringTokenizer;
import android.util.Log;

public class Main {
    private final static String TAG = "TorUtils";
    public final static String SHELL_CMD_PS = "ps";
    public final static String SHELL_CMD_PIDOF = "pidof";

    public static int findProcessId(String command) {
        int procId = -1;

        try {//  w w  w. j a v a2 s . c om
            procId = findProcessIdWithPidOf(command);

            if (procId == -1)
                procId = findProcessIdWithPS(command);
        } catch (Exception e) {
            try {
                procId = findProcessIdWithPS(command);
            } catch (Exception e2) {
                Log.e(TAG, "Unable to get proc id for command: "
                        + URLEncoder.encode(command), e2);
            }
        }

        return procId;
    }

    public static int findProcessIdWithPidOf(String command)
            throws Exception {

        int procId = -1;

        Runtime r = Runtime.getRuntime();

        Process procPs = null;

        String baseName = new File(command).getName();
        // fix contributed my mikos on 2010.12.10
        procPs = r.exec(new String[] { SHELL_CMD_PIDOF, baseName });
        // procPs = r.exec(SHELL_CMD_PIDOF);

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                procPs.getInputStream()));
        String line = null;

        while ((line = reader.readLine()) != null) {

            try {
                // this line should just be the process id
                procId = Integer.parseInt(line.trim());
                break;
            } catch (NumberFormatException e) {
                Log.e("TorServiceUtils", "unable to parse process pid: "
                        + line, e);
            }
        }

        return procId;

    }

    public static int findProcessIdWithPS(String command) throws Exception {

        int procId = -1;

        Runtime r = Runtime.getRuntime();

        Process procPs = null;

        procPs = r.exec(SHELL_CMD_PS);

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                procPs.getInputStream()));
        String line = null;

        while ((line = reader.readLine()) != null) {
            if (line.indexOf(' ' + command) != -1) {

                StringTokenizer st = new StringTokenizer(line, " ");
                st.nextToken(); // proc owner

                procId = Integer.parseInt(st.nextToken().trim());

                break;
            }
        }

        return procId;

    }
}

Related

  1. executeCommand(String command)
  2. normalShell(String normalCommand)
  3. runAsRoot(String[] commands)
  4. runSimpleCommand(String command, String directory)
  5. doShellCommand(String[] cmds, StringBuilder log, boolean runAsRoot, boolean waitFor)
  6. findProcessIdWithPidOf(String command)
  7. findProcessIdWithPS(String command)
  8. execRootCmd(String cmd)
  9. execRootCmdSilent(String cmd)