Monitors and exits

Contents
monitor/2
'DOWN' messages
demonitor/1
Links or monitors

monitor/2

The monitor function watches a pid from the outside. It is one-directional and needs no mutual agreement, unlike a link.

When the watched process dies, the watcher receives a DOWN message with a reference that identifies the monitor.

1> Pid = spawn(fun() -> ok end).

<0.123456789.0>

2> Ref = erlang:monitor(process, Pid).

#Ref<0.123456789.0>

'DOWN' messages

The DOWN message carries the reference, the watched pid, and the exit reason. Match on the tag and the reference to tell monitors apart.

Two monitors on the same process are two separate DOWN messages with two references.

1> Pid = spawn(fun() -> exit(why) end).

<0.123456789.0>

2> Ref = erlang:monitor(process, Pid).

#Ref<0.123456789.0>

3> receive

{'DOWN', Ref, process, Pid, Reason}

why

demonitor/1

The demonitor function stops a monitor before it fires. Use the flush option to also remove a DOWN message already waiting in the mailbox.

Monitoring a dead process is safe: the DOWN has already been delivered or is on its way.

1> Pid = spawn(fun() -> timer:sleep(1000) end).

<0.111.0>

2> Ref = erlang:monitor(process, Pid).

#Ref<0.111.0>

3> erlang:demonitor(Ref, [flush]).

true

Links or monitors

Links fail together: use them inside a supervision tree that should die as one unit. Monitors observe without being touched: use them for one-way watching.

When in doubt, monitor. Links belong to structure, monitors to observation.

Next: GenServer

Article author: Arthur Isaev

Related articles
Erlang
Erlang course: zero to hero in 50 lessons
What is Erlang and where it runs
Install Erlang and check the version
erl: the interactive shell
Your first Erlang script
Variables and single assignment
Numbers
Atoms
Strings and binaries
Truthiness: only false and nil
Conditions: case and if
Recursion replaces loops
Lists
Tuples
Maps
Functions and clauses
Pattern matching
Guards
Records
Exceptions: try and catch
Modules, exports and imports
Funs: anonymous functions
Higher-order functions
Keyword lists
Erlang idioms and clean code
Rebar3 builds
Testing with EUnit
Debugging
Encodings and Unicode
Time and date
JSON and term formats
Processes and message passing
Spawn and links
Monitors and exits
GenServer
Supervisors
OTP applications
Files and IO
Sockets
HTTP clients
io:format and output
Let it crash: error handling
ETS tables
Timers and timeouts
Code loading
Distributed Erlang
Capstone: todo CLI
Capstone: TCP server
Performance
Modern Erlang tour
Hero roadmap: the whole course on one page
Install Erlang on Windows 11
Install Erlang on Ubuntu
Install Erlang on Rocky Linux
Install Erlang on macOS
Install Erlang on FreeBSD
Erlang in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: