Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a list of running processes and killing a specific process

I'm executing the following piece of code:

ActivityManager actvityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<RunningTaskInfo> procInfos = actvityManager.getRunningTasks(1000);

Then, I kill one process I'm interested in with

actvityManager.killBackgroundProcesses(process.baseActivity.getPackageName());

where process is an entry from procInfos.

The problem is if I run getRunningTasks again - it would still show the process I (presumably) killed, while a task manager for Android listed that process before calling killBackgroundProcesses and removed it from list after that call.

So, any ideas on how does task manager get its list of running processes? And is it normal that I have successfully killed 3rd-party process on an unrooted device?

like image 408
Violet Giraffe Avatar asked Mar 23 '12 12:03

Violet Giraffe


People also ask

How do I kill multiple processes at once?

killall Command – kill the processes by name. By default, it will send a TERM signal. The killall command can kill multiple processes with a single command. If more than one process runs with that name, all of them will be killed.

How do you see the PID of all the running processes and how do you kill a particular process in Linux?

You can list running processes using the ps command (ps means process status). The ps command displays your currently running processes in real-time. This will display the process for the current shell with four columns: PID returns the unique process ID.

How do you find a process and kill it?

The killall command is used to kill processes by name. By default, it will send a SIGTERM signal. The killall command can kill multiple processes with a single command.

How do I kill a group of processes?

If it is a process group you want to kill, just use the kill(1) command but instead of giving it a process number, give it the negation of the group number. For example to kill every process in group 5112, use kill -TERM -- -5112 .


1 Answers

'Running' doesn't mean that a user started it; it might be listening for events, doing a scheduled background sync or anything else.

ActivityManager.getRunningTasks() might be closer to what you want, but in essence you are always going to have this problem, because the user is not in full control over what is currently active.

like image 70
Rob Pridham Avatar answered Sep 22 '22 16:09

Rob Pridham