简体   繁体   中英

Android killing Process using only PS and GREP in shell script

I really need help here... I am writing a shell script, which starts a process then kills it every 10 seconds then restarts it again.

I understand that using 'ps' command will show all the processes that are running and you can kill it by running 'kill [pid]'. However, grepping the PID of the process that I am launching is not easy.

I've looked everywhere and for some reason, I cannot use functions like pgrep, awk, xargs, pidof... They are just not found...

I can only think of a way where I have to output the ps file then parse it and grab the PID alone.. but that seems too much...

Can anyone help me? I think I am only limited to using ps and grep only...

I am launching the phone application by running am start -a android.intent.action.CALL -d tel:XXX-XXX-XXXX Then by running 'ps m.android.phone' I can use kill [PID] to stop call the call

I've also tried running commands like pm clear com.android.phone, adb shell am force-stop com.android.phone but none of them would stop the call...

Please help Thanks a lot!

Use

am kill package-name

or

am force-stop package-name

replacing package-name by the name of the application you want to kill.

If you know the process name of the application, then I wrapped together a very quick and dirty script to parse the PID of the process:

ps -u $(whoami) | grep firefox | awk '{printf $1}'

You should obviously replace firefox with your process name of choice.

Please note that I am no expert on the area, but it works on my end.

您可以尝试命令pidof直接获取进程ID,eks:

pidof apashe2

跟随马库斯说:

adb shell kill -9 $(adb shell ps | grep firefox | awk '{print $1}')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM