How can I force-stop all user applications that are running?


Question

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.


Answer

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.


Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes