Check if Process Exists

Post your questions and help other users.

Moderator: Martin

Post Reply
User avatar
digitalstone
Posts: 342
Joined: 21 Oct 2017 12:36
Location: The Netherlands

Check if Process Exists

Post by digitalstone » 06 Aug 2018 18:45

Since the condition "App Process Running" mostly doesn't work anymore from Android 7, I'm trying out manually to check upon the existence of a process.
Now i've tried the "Execute Command" and its Root-version with some commands (like: ps | grep com.privateinternetaccess.android), but i'm wondering if there is a better method.
Or is this the best one?

And if so:
What to do with the complete {stdout} variable?
Mainly a whole bunch of numbers (of which 1 i bet is a PID) come along which may be useful if i knew how to read them.
I can't show you the {stdout} yet since i don't know if they are personable numbers..
Phone: LG Nexus 5X (rooted vanilla Android 7.1.2)

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Check if Process Exists

Post by Desmanto » 07 Aug 2018 16:59

Yeah, that condition is totally broken in NG 7.0+. I only use that once, so i just realized it doesn't work anymore in oreo too. AFAIK, it is google restriction, to prevent app from spying other app process.

So the solution is only using execute root command. You can use ps -A | grep com.privateinternetaccess.android and then parse the output using regex.
The ps can give quite a different data, so you need to try at your phone to check it out.

But I have tried a while, there seems to be better way using pgrep.

Code: Select all

pgrep com.privateinternetaccess.android
If the process only one copy, you can directly get the PID. But if it returns 1, then maybe there are several process with the same name. At mine, I need to filter it out using

Code: Select all

pgrep -xf com.privateinternetaccess.android
Which will filter out exactly that process name. x param is to match exactly, while f to match only to process name. You might need to change this param to see the result.

Since we got the PID, we can check its oom to determine it is foreground app or background services. Let take example it is 1234

Code: Select all

cat /proc/1234/oom_adj
This will give value between -17 to 17. Where if it foreground or backgrouind services, it should give 1 or 2. (still investigating what it means).
Since the 1234 is retrieved from pgrep, we can pipe the command to single execute root command

Code: Select all

cat /proc/$(pgrep -xf com.privateinternetaccess.android)/oom_adj
This will directly give you the oom_adj value of com.privateinternetaccess.android. Check stdout if it is 1 or 2, then the service is running. You can then use expression to branch your flow accordingly.

BTW, I just pull this out from googling and testing, I might still wrong. So let's try it out to see if it works.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

User avatar
digitalstone
Posts: 342
Joined: 21 Oct 2017 12:36
Location: The Netherlands

Re: Check if Process Exists

Post by digitalstone » 09 Aug 2018 03:23

I tried and tried but the commands starting with 'ps' just won't work (using "Execute Root Command")

Depending on if/where i will place the '|' sign (because maybe you forgot about that one??.. i don't know), an error output will be generated, with or without an {stdout} stating the error-content.
The only thing that works is the one i already used (ps | grep com.privateinternetaccess.android). I guess some phones/systems require slight different input.

But no matter, i already can read a true VPN connection within my current generatable {stdout}, and it works :)

Thanks for you extensive help Desmanto.
Phone: LG Nexus 5X (rooted vanilla Android 7.1.2)

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Check if Process Exists

Post by Desmanto » 09 Aug 2018 15:38

Yeah, different results. As I try in bliss oreo, I don't need the -A, simply ps. But at phone, I need to use ps -A. Probably because I have busybox at phone.

This method seems to be the working workaround currently. Glad to see it is working. Might be useful for my other flow one day.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Post Reply