Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Running Processes List and Kill Their Background Services

I am building an Android app for RAM optimization. I can successfully get the list of running processes (and their PIDs) using this answer. However, I don't see a way to kill them or their background services by PID.

like image 944
Dzhuneyt Avatar asked Oct 15 '12 09:10

Dzhuneyt


People also ask

How do I find out what background processes are running?

The jobs command will show any background jobs started within the current shell, usually by starting a background task with the & operator or ^Z bg (e.g. sleep 10 & ). If you want to see all of the background processes running on the system, you can use ps -e , or ps -eF to get some additional details.

How do I get a list of running processes?

To list currently running processes, use the ps , top , htop , and atop Linux commands. You can also combine the ps command with the pgrep command to identify individual processes.

How do you kill background running job?

Killing a background process is fairly straightforward; use the command pkill and the process ID, or process name as: Using the pkill command will force terminate (-9) the processes with the process name of ping.


1 Answers

It turned out to be something very basic:

ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

for (RunningAppProcessInfo pid : am.getRunningAppProcesses()) {
    am.killBackgroundProcesses(pid.processName);
}
like image 172
Dzhuneyt Avatar answered Sep 28 '22 10:09

Dzhuneyt