Force stop (or kill) a process on Android without root: part 2

Force stop (or kill) a process on Android without root: part 2

As Android does not provide decent means for process management many users are seeking a workaround. One of them is implemented in Hibernate - an Android utility for process monitoring and hibernation.

Stopping or killing unwanted applications becomes more and more difficult task with every new Android version. For Android 4-5 Hibernate provides an experimental feature for "force stop" automation which was described in the article "Hard"-killing background processes on Android without root. It's based on adb shell scripting. Unfortunately, in Android 6 it does not work anymore because of missing notify command. Actually, it can be missing even in specific manufacturer's versions of Android 4-5 othen than on reference Google devices.

After some investigation I've finally found another way for interaction between the shell script and the app which utilizes tail commnd. It's not as effective as notify and may result in additional CPU consumption, but we can do nothing with this thanks to Google. I'm posting the new wuh_notify.sh file here:

#!/bin/sh output=`ps com.weborienteer.utilits.hibernate | grep hibernate` set -- $output echo $2 > /data/local/tmp/wuh_pid.txt file=/data/local/tmp/wuh_pkg.txt touch "$file" # tail -f file monitoring is not blocking yet in adb, marked as TODO var0=`tail -f $file` while true; do var1=`tail -f $file` if [ "$var0" != "$var1" ]; then am force-stop $var1 var0=$var1 fi sleep 1 done

The source code requires some notes.

One more obstacle which Google introduced in Android 6 is how ps shell command works. Now it lists only processes of regular users, and skips shell processes. This is essential for the hibernation workaround because Hibernate should know if the shell script is running. The original script (see Part 1) writes its own PID into the wuh_pid.txt file, and Hibernate checks this process to enable the feature. Now the shell PIDs are hidden for the app, so a different trick is used: the script writes the app PID into the file. This is a less reliable approach because the installed and running script will be treated as missing if Hibernate itself is restarted. As a result you may need to repeat some steps from the installation procedure (see Part 1). I'll try to remedy this problem in future.

Next, tail -f file monitoring feature is not yet fully supported in adb shell: the option is available but does not work as expected. This is why we need to run tail -f in a loop. sleep 1 denotes the interval in seconds between consecutive checkups of automation commands arriving from Hibernate via wuh_pkg.txt file. Enlarge this number to lower CPU consumption at the expence of responsiveness. Please note that hibernation of the same package twice in a row is impossible, because the code requires the name to change.

Finally, I'd like to announce a feature which is being tested for the future release, that if the "force stop" automation script does not succeed in stopping a process during twice as many attempts specified in Attempts count in Hibernate Settings dialog, then the script automation is automatically switched off. This is done so to prevent potentially infinite races with some processes which Android likes to recreate till the very end.

2 comments:

  1. Hi Stan,

    As an amateur Android user it's not obvious how the source code for the new wuh_notify.sh is converted into a file ready for use. Could you make a download link the way you in part 1?
    Do I just substitute the original wuh_install.sh with the new one? Would really appreciate it if you could give some directions as how to load the script.

    Best regards

    Fretho

    ReplyDelete
  2. Yes, you're right - substitute the former file with the new one. You may create new empty file and copy/paste the code into it as text or edit existing file (select all, delete, paste the code). Make sure to use Linux newlines convention in the editor.

    ReplyDelete