r/tasker • • Jun 23 '23

Looking for task to kill all running background applications

Not quite sure how to do this using ADB WiFi with no root. Basically I just need it to query all applications currently running in the background and return them in a list so that I can use Kill or am force-stop app package to stop it. For this to work right, I would need the app package name parsed out for each app stored in an array, and nothing more.

Anyone know a way to do this with Tasker? I am always connected to ADB TCP port 5555 so anything requiring that is okay.

The goal here is to prep my phone for intense gaming. Not interested in a profile since profiles require resources which I'm trying to limit.

Edit:

So thanks for your input guys. If you could take a look at my custom made task that is designed to be shared. It gets the background packages and runs it through an app info action to display the app names in a list. You can also setup an application ignore list which will ignore any applications in it. The list can be updated fairly easily just by running the task and pressing available options.

My concern is that the applications it's querying aren't actually running in the background or they aren't being killed properly.

Feedback requested. App Kill Booster (TaskerNet Import)

7 Upvotes

12 comments sorted by

2

u/Blitzdroids Jul 01 '23

So thanks for your input guys. If you could take a look at my custom made task that is designed to be shared. It gets the background packages and runs it through an app info action to display the app names in a list. You can also setup an application ignore list which will ignore any applications in it. The list can be updated fairly easily just by running the task and pressing available options.

My concern is that the applications it's querying aren't actually running in the background or they aren't being killed properly.

Feedback requested. App Kill Booster (TaskerNet Import)

4

u/agnostic-apollo LG G5, 7.0 stock, rooted Jun 24 '23 edited Jun 24 '23

The pm list packages -3 command can be used to list 3rd party apps as shown above. The -a flag will match uninstalled and secondary user packages as well, which is not required for your case.

To check if an app is running, simply running pidof '<package_name>' should work, but more reliable way would be something like dumpsys activity processes '<package_name>' | grep -A5 -aE 'All known processes:' | grep -aE 'ProcessRecord{', like dumpsys activity processes 'net.dinglisch.android.taskerm' | grep -A5 -aE 'All known processes:' | grep -aE 'ProcessRecord{' since that will list processes state based on android internal records. Apps can have multiple app processes and they can change process names as well.

Using ps and pidof isn't reliable because any app/process can start processes with the name of an app package name, which will then show in the output, giving a false positive, unless you check the uid as well.

ACTIVITY MANAGER RUNNING PROCESSES (dumpsys activity processes)
  All known processes:
  *APP* UID 10300 ProcessRecord{cb2c4e8 31695:net.dinglisch.android.taskerm/u0a300}
    user #0 uid=10300 gids={3002, 3003, 3001, 50207, 20207, 9997}
    mRequiredAbi=arm64-v8a instructionSet=arm64
    class=com.joaomgcd.tasky.TaskyApp

https://developer.android.com/guide/topics/manifest/service-element#proc

https://cs.android.com/android/platform/superproject/+/android-13.0.0_r54:frameworks/base/services/core/java/com/android/server/pm/PackageManagerShellCommand.java;l=866

https://cs.android.com/android/platform/superproject/+/android-13.0.0_r54:frameworks/base/core/java/android/content/pm/PackageManager.java;l=1031

https://cs.android.com/android/platform/superproject/+/android-13.0.0_r54:frameworks/base/core/java/android/content/pm/PackageManager.java;l=1111

u/Ratchet_Guy u/HunterXProgrammer

1

u/HunterXProgrammer Jun 24 '23 edited Jun 24 '23

Great info! Removed the redundant -a flag and checking out the articles.

Also, I'd like to implement using the kill command but ADB Wifi kill <PID> gives an Operation not permitted error.

Currently stuck with using am force-stop instead.

2

u/agnostic-apollo LG G5, 7.0 stock, rooted Jun 24 '23

That would be because adb shell user doesn't have direct access to kill app processes. And it's better to use force-stop since android has specialized mechanisms for gracefully killing app processes (Can provide links on request). If you use kill, then it can result in data loss too since app may not have saved its data to disk like shared preferences, etc.

Also apps may get restarted automatically after force stopping, like on broadcasts or probably on alarms and should be taken into consideration for why killing is being done in the first place.

1

u/HunterXProgrammer Jun 24 '23

Thanks for pointers, I'll keep the am force-stop then, instead of kill, since Android prefers that.

1

u/agnostic-apollo LG G5, 7.0 stock, rooted Jun 24 '23

Welcome.

3

u/Ratchet_Guy Moderator Jun 24 '23

/u/agnostic-apollo probably has a Shell Command or something to get that list :)

3

u/agnostic-apollo LG G5, 7.0 stock, rooted Jun 24 '23

You probably think I have a shell command for everything 😂

3

u/HunterXProgrammer Jun 24 '23 edited Jun 24 '23

Since forever, whenever I use multiple lines in [ADB Wifi] action, Tasker sort of chokes out 3 of 10 times and the action just times out . I've learned to live with using one-liners.

Here is a one-liner I'd use -

whitelist="net.dinglisch.android.taskerm,com.termux" && echo $(/system/bin/ps -e -o USER,NAME | grep -E "^u0_a" | sed 's/.* //g' | grep -Eo "(^$(echo $(pm list packages -3 | sed 's/package://g' | grep -Ev "(^$(echo $whitelist | sed 's/ *//g' | sed 's/\./\\\./g' | sed 's/,/\$\|\^/g')$)") | sed 's/\./\\\./g' | sed 's/ /\$\|\^/g')$)") | sed 's/ / \& am force-stop /g' | sed -E 's/^/am force-stop /' | sed -E 's/$/ \&/' | sh && wait

The whitelist contains comma seperated package names you'd like to avoid killing.

This one-liner will only get the list of user installed apps (we avoid system apps) and check if it's running and proceed to kill the app if it's running.

1

u/The_IMPERIAL_One S24 Ultra (A16) | realme GT NEO 3 (A14) Sep 06 '23 edited Sep 06 '23

Hey Hunter,

Since forever, whenever I use multiple lines in [ADB Wifi] action, Tasker sort of chokes out 3 of 10 times and the action just times out.

Does that implies no return in the output i.e. %aw_output is !SET? I faced this too and reported about it to u/joaomgcd.

Btw, do you have any efficient one-liner code to install apks using adb wifi? I'd love to look at one of your creation.

1

u/MarketingPlane2114 Dec 05 '25

Can I get the code for all system apps and installed apps kill?

1

u/EveryDiet2664 Feb 22 '24

Extremely useful. Thanks a lot. Sorted out my issue with new firestick 4max as store app only allow to close one by one the background apps