0

I need a way to kill all the processes used in the script below. I removed all the unnecessary code and made it as simple as I can, without ruining the structure.

I made the kill_other function for that, which when called (after CTRL + C is pressed) is suppose to kill everything.

Right now if I kill the script, functionOne and adb logcat continue running.

How can I kill them?

Also, since I'm fairly new to the trap function I have to ask have I positioned it correctly in the code?

EDIT : I am running this script on Ubuntu 19.04 and my target is an Android phone, if someone needs the info.

#!/bin/sh

kill_other(){
    ## Insert code here to kill all other functions
    exit
}

trap 'kill_other' SIGINT

main(){

    functionOne &

    adb logcat > log_test.log &

    while true
        do
            echo "==================================================================="
            memPrint
            sleep 10
        done
}

functionOne(){
    while true
        do
            sleep 20
            echo "==================================================================="
            echo "Starting app 1"
            echo "==================================================================="
            functionTwo

            sleep 20
            echo "==================================================================="
            echo "Starting app 2"
            echo "==================================================================="
            functionThree
        done
}

functionTwo(){
    adb shell monkey -p com.google.android.youtube -c android.intent.category.LAUNCHER 1
}

functionThree(){
    adb shell monkey -p tv.twitch.android.app -c android.intent.category.LAUNCHER 1
}

memPrint(){
    adb shell dumpsys meminfo | grep -A 10 "Total PSS by process\|Foreground\|Perceptible\|Total RAM\|Home"
}

## Start
main
  • I'm not sure this question makes sense. `adb` isn't a bash process *at all* (it's a standalone UNIX executable) – Charles Duffy Sep 26 '19 at 16:24
  • 1
    That said, if you want to be able to kill processes, collect their PIDs when you start them! `adb logcat > log_test.log & adb_logcat_pid=$!`, and `functionOne & functionOne_pid=$!`, then you can `kill "$functionOne_pid"` and/or `kill "$adb_logcat_pid"`. – Charles Duffy Sep 26 '19 at 16:25
  • @CharlesDuffy For some reason I am unable to write pid to a variable.. anyways after digging through those posts and realizing I put `sh` instead of `bash` in my first line, which caused my traps not to work (SIGINT doesn't work in sh, INT does), I finally found the solution.. `trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT` works like a charm. Thanks for the replies! – Aleksandar Čolović Sep 26 '19 at 18:07
  • If you happen to ask a question that includes a [mcve] letting others reproduce that failure to assign the PID to a variable, then feel free to @-notify me; I'd be happy to take a look. – Charles Duffy Sep 26 '19 at 19:16

1 Answers1

0

You might try doing a "ps -ef" grepping the results for "adb", or if for instance the bin is actually adb you can try "pkill adb".

or as I saw around (Android ADB stop application command like "force-stop" for non rooted device)

adb shell ps => Will list all running processes on the device and their process ids

adb shell kill <PID> => Instead of use process id of your application

This are the options to place in kill function, just to clarify

Regards !

Simson
  • 3,373
  • 2
  • 24
  • 38
tavanez
  • 13
  • 7
  • Thanks for the response! I've tried killing all the adb processes using `sudo kill -9 $(pgrep adb)`. It kills them all, but adb instantly restarts and the process continues trying to switch apps. – Aleksandar Čolović Sep 26 '19 at 15:39
  • IDK the OS but if it runs with systemd, try to give a look in service config, it might be prepared for that and restarts n times in n seconds. Or check with cron or automation tools that might be running – tavanez Sep 26 '19 at 15:54
  • I am running my script on Ubuntu 19.04 and I'm targeting an Android Phone. As far as everything else you wrote... I don't understand it, or rather what I should do.. – Aleksandar Čolović Sep 26 '19 at 16:20
  • 1
    Boo, hiss -- `pgrep` and `pkill` are huge hammers that can kill processes unrelated to your script; if you're only trying to kill things you yourself started, you get the PIDs when you start them, and can use those later to do more targeted `kill`s. – Charles Duffy Sep 26 '19 at 16:26
  • @CharlesDuffy or just uses the proper tool as mentioned on other topic. The adb shell ps to list and adb shell kill to kill them. – tavanez Oct 22 '19 at 22:02