When using an Erlang supervisor, can I execute code in response to a child exiting?

StackOverflow https://stackoverflow.com/questions/23410551

  •  13-07-2023
  •  | 
  •  

When the child exits (either normally or on an error) I want to take some actions. Do I use erlang:monitor/2? And if so how do I get the pid? Also, will I be able to see why it stopped?

有帮助吗?

解决方案

Yes, monitor will do the job.

You will get back: {'DOWN', MonitorRef, Type, Object, Info}

Where the Object is the pid and Info is the exit reason

You can also do with link and trap_exit, but links are bi-directional so you'd take that child out with you if the parent died.

The easiest way to get the PID of the child process will be to have the child register itself with the listener (the one using monitor) when it starts. Have it send self() and then the listener can monitor that PID.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top