jdesilvio
jdesilvio

Reputation: 1854

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?

Upvotes: 17

Views: 10503

Answers (1)

Gazler
Gazler

Reputation: 84180

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.

Upvotes: 28

Related Questions