3

I found that first of all System>Apps>Running is not helpful always and secondly I we stop the services, it may star again if corresponding application is running. The correct way to stop application is force stop. (that's why ram booster may not helpful sufficiently).

I've made an simple script that get the list of all user applications and then force-stop all one by one:

pm list packages -3 -f | sed /jackpal.androidterm/d > /sdcard/tbs/app2fs

while read -r line; do APK=$(echo $line | cut -d ':' -f2 | cut -d '=' -f1); PKG=$(echo $line | cut -d '=' -f2); echo "Forcing to Stop: $(aapt d badging $APK | grep "application: label" | cut -d "'" -f2)"; am force-stop $PKG; done < /sdcard/tbs/app2fs

The draw back of above command is it tries to force-stops all (doesn't matter running or not) third-party applications. But I want to do am force-stop only for the running user apps.

So, How do I get the list of all running user apps (not services)?

You may have better way to do this than my script/command, hence broadly: How can I force-stop all user applications that are running?

Note: I recommend the command-line solution. Because this type application (that can force-stop all running user apps) mostly runs constantly for monitoring which I don't like. If you've a suggestion for an app that can do such job and also favourable (should not run constantly for monitory) , then you can suggest it.

0

1 Answer 1

2

Note: This solution:


If an app is running (be it in background or foreground) then it must have a process assigned to it. In that case, it should show up under the radar of ps command. We can single out apps' processes from ps command and compare them with the list stored from pm command. Whatever comes out as common can be safely killed.

Here's a basic script to do that:

#!/system/bin/sh

HOME="/data/media/0";
mkfifo "$HOME"/ps "$HOME"/packages;
/system/bin/ps | grep 'u0_' | awk '{print$9}' | sort -o "$HOME"/ps &
pm list packages -3 | sed 's/package://g' | sort -o "$HOME/"packages &
comm -12 "$HOME"/ps "$HOME"/packages | while read package; do am force-stop "$package"; printf "Force-stopped: $package\n"; done
rm "$HOME"/ps "$HOME"/packages;

This script must be run with root privilege.

In that script:

  • I'm creating two named pipes, namely ps and packages under /data/media/0. (It is optional to do. You can safely remove the lines starting with mkfifo and rm). I'm relying on the output provided by native ps tool. If an app has a process, then its package name would be shown under the column NAME. I'm using grep to filter out processes running under user 0 (your user account has user ID 0) and then using awk to print the 9th field. That field in each line would show a package name.

    Note that you can use Busybox's ps tool. It has a feature to show only a particular column. You'll have to tweak the script a bit but you can do away with hard-coded dependency (a particular field in a line).

  • I'm storing a list of user-installed packages (third-party) using pm tool.
  • I'm now comparing the content of ps and packages and listing only the common lines. Note that I used sort in earlier commands because comm requires a sorted file.

    Each common line then goes through a loop where the line (a package name) is used to force-stop the corresponding package.

Note: am force-stop is useless against a device administrator app.

1
  • How did I miss ps!
    – Pandya
    Feb 22, 2016 at 12:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .