[erlang-questions] Escript and spawn

Matthias Lang <>
Sat Apr 28 09:23:38 CEST 2007

Erik de Castro Lopo writes:

 > The problem is that the spawn command doesn't seem to agree with
 > escript. Here's an example:
1 >     #!/usr/bin/env escript
2 >     
3 >     -module (test).
4 >     -export ([ main/1, new_process/0 ]).
5 >     
6 >     new_process () ->
7 >         io:format ("new_process\n"),
8 >         exit ("Done").
9 >     
10>     main (_) ->
11>         spawn (?MODULE, new_process, []),
12>         timer:sleep (5000),
13>         ok.

 > Can anyone clue me in as to why?

I've never used Escript before. I don't know if the R11B-2 version
you're using comes with a manual or not, but R11B-4 does. It's here:


The first thing I notice is "there should not be any module
declaration in an Erlang script file". So we delete line 3.

The second thing is "It is not necessary to export the main/1
function.", so zap line 4 as well. That causes the problem that
spawn/3 wants an exported function. But there's no reason to use
spawn/3 in this case, so modifiy line 11 to look like this:

   spawn(fun() -> new_process() end),

this conveniently avoids using the preprocessor's ?MODULE, which
apparently isn't allowed in escript ("Pre-processor directives in the
script files are ignored" I'm not sure if a macro counts as a
pre-processor directive or not, but whatever, it doesn't work.).

So let's try it:

   ~ >cat test
   #!/usr/bin/env escript
   new_process () ->
     io:format ("new_process\n"),
     exit ("Done").
   main (_) ->
     spawn(fun() -> new_process() end),
     timer:sleep (5000),
   ~ >./test

i.e. it works as expected. I used Erlang R11B-4 on debian.


All of this left me with a "but how does this stuff actually work?"
feeling. It doesn't look too complicated. A glance at 'escript.erl',
especially the interpret() function suggests that it does partial
pre-processing and then interprets the result with erl_eval. So there
is no module (and, even in compile mode, the module is not related to
the filename).

The erl_eval manpage in R11B-4 claims that receive "cannot be handled
properly". A basic test of receive, both in escript and using erl_eval
directly, suggests that it works. Are the problems, or is it just an
out of date manpage?


More information about the erlang-questions mailing list