Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir - Get all PIDs for processes under a supervisor

I have a Supervisor and want to know all the processes running under that Supervisor at any given time. It seems like there should be an easy way to get all PIDs, names, etc. for all processes under a Supervisor or in a node, but I can't find anything.

Any suggestions for how to do this?

like image 626
jdesilvio Avatar asked Mar 17 '16 14:03

jdesilvio


1 Answers

You can use Supervisor.which_children/1:

iex> Supervisor.which_children(MyApp.Supervisor)
[{MyApp.SubSupervisor, #PID<0.1695.0>, :supervisor, [MyApp.SubSupervisor]},
 {MyApp.Endpoint, #PID<0.1686.0>, :supervisor, [MyApp.Endpoint]}]

Returns a list with information about all children of the given supervisor.

Note that calling this function when supervising a large number of children under low memory conditions can cause an out of memory exception.

This function returns a list of {id, child, type, modules} tuples, where:

  • id - as defined in the child specification

  • child - the PID of the corresponding child process, :restarting if the process is about to be restarted, or :undefined if there is no such process

  • type - :worker or :supervisor, as specified by the child specification

  • modules - as specified by the child specification

Since the type and pid are provided you can recursively fetch the children to generate a list of all the pids if required.

like image 108
Gazler Avatar answered Nov 08 '22 03:11

Gazler