android - How to get pid from binary which the run via getRuntime().exec -
how pid binary run via getruntime().exec. want pid /data/data/com.tes.tes/binary
my code run service is:
myexecshell("/data/data/com.tes.tes/binary"); public void myexecshell(string cmd) { process p = null; try { p = runtime.getruntime().exec(cmd); p.waitfor(); } catch (exception e) { // todo: handle exception } }
if run command ps | grep binary
result:
app_96 12468 1 1176 680 c0194d70 0007efb4 s /data/data/com.tes.tes/binary
i want pid, how it? have tried this:
activitymanager manager = (activitymanager) getsystemservice(context.activity_service); list<runningappprocessinfo> list = manager.getrunningappprocesses(); if (list != null) { (int = 0; < list.size(); ++i) { log.d("dlog", list.get(i).tostring() + "\n"); if ("/data/data/com.tes.tes/binary" .matches(list.get(i).tostring())) { int pid = android.os.process.getuidforname("/data/data/com.tes.tes/binary"); log.d("dlog","pid: "+pid); } } }
but not success.
thanks.
the problem is, running process not app context. can try fetch pid standard linux methods:
private int getpid() { int pid = -1; process p = null; try { p = runtime.getruntime().exec("ps"); p.waitfor(); inputstream = p.getinputstream(); bufferedreader r = new bufferedreader(new inputstreamreader(is)); string s; while ((s=r.readline())!= null) { if (s.contains("/data/data/com.tes.tes/binary")) { // todo pid ps output // " | awk '{ pring $2 }' // pid = something; } } r.close(); } catch (exception e) { // todo: handle exception } return pid; }
Comments
Post a Comment