To increase the likelihood of a terminate callback, the server process must catch the outputs. However, even with this, the callback cannot be called in some situations (for example, when the process is brutally killed or when it itself crashes). See here for more details.
As already mentioned, if you want to politely shut down your system, you must call :init.stop , which will recursively terminate the control tree that calls the terminate callbacks.
As you noticed, there is no way to catch the sudden exits of the BEAM OS process from the inside. This is a self-defining property: the BEAM process ends abruptly, so it cannot run any code (since it is completed) π. Therefore, if BEAM is brutally terminated, the callback will not be called.
If you unconditionally want to do something when BEAM dies, you need to discover it from another OS process. I am not sure what your specific use case is, but provided that you have some strong needs for this, then running another BEAM node on the same (or different) machine may work here. Then you can have one process on one node monitoring another process on another node, so you can respond even if BEAM is brutally killed.
Nevertheless, your life will be simpler if you do not need to unconditionally run some cleaning logic, so think about whether the code in terminate mandatory, or rather pleasant.
sasajuric
source share