Links, Exit Trapping & 'Let It Crash'
Use link and trap_exit to detect process death and recover.
Code
-module(keeper).
-export([start/0, child/0]).
start() ->
process_flag(trap_exit, true), % convert EXIT signals to messages
Pid = spawn_link(fun child/0), % spawn AND link atomically
keeper_loop(Pid).
keeper_loop(Pid) ->
receive
{'EXIT', Pid, Reason} ->
io:format("child died with ~p, restarting~n", [Reason]),
NewPid = spawn_link(fun child/0),
keeper_loop(NewPid);
{'EXIT', _Other, _Reason} ->
%% some other linked process died; ignore
keeper_loop(Pid);
{stop, From} ->
exit(Pid, shutdown),
From ! ok
end.
child() ->
timer:sleep(rand:uniform(5000)),
case rand:uniform(3) of
1 -> erlang:error(bad_thing); % crash on purpose
2 -> exit(normal); % 'normal' exits don't propagate
_ -> exit(bad_luck) % abnormal exit
end.
% With trap_exit=true the keeper survives all of these because
% exit signals arrive as {'EXIT', Pid, Reason} messages.Explanation
link/1 creates a bidirectional link so that if either process dies the other receives an exit signal — by default that signal crashes the receiver too. Setting process_flag(trap_exit, true) converts those signals into {'EXIT', From, Reason} messages, letting the keeper observe crashes without dying itself. This 'let it crash' style — keep the supervisor simple, let workers fail fast — is the heart of Erlang's fault tolerance.
More Erlang Snippets
Ping-Pong Processes
Two processes pass messages back and forth using spawn and receive.
gen_server Counter
Build a stateful server with the gen_server behaviour.
Supervisor Tree
Define a supervisor that starts workers and restarts them on crash.
Selective Receive with References
Match a specific reply out of many messages using a unique reference.
Stateful Server via Tail Recursion
Hold mutable state in a process by threading it through recursive calls.
Parallel Map with rpc:pmap
Apply a function to each list element in parallel across processes.