Lee
Lee

Reputation: 31090

How do I send message from the command line to an Erlang process?

I am trying to notify an Erlang process that an external program (a Matlab script) has completed. I am using a batch file to do this and would like to enter a command that will notify the Erlang process of completion. Here is the main code:

In myerlangprogram.erl:

runmatlab() ->
      receive
           updatemodel->
               os:cmd("matlabscript.bat"),
...
end.

In matlabscript.bat:

matlab -nosplash -nodesktop -r "addpath('C:/mypath/'); mymatlabscript; %quit;"
%% I would like to notify erlang of completion here....
exit

As you can see I am using the 'os:cmd' erlang function to call my matlab script.

I am not sure that this is the best approach. I have been looking into using ports (http://www.erlang.org/doc/reference_manual/ports.html) but am struggling to understand how/where the ports interact with the operating system.

In summary, my 2 questions are: 1. What is the easiest way to send a message to an Erlang process from the command line? 2. Where/how do erlang ports receive/send data from/to the operating system?

Any advice on this would be gratefully received.

N.b. the operating system is windows 7.

Upvotes: 2

Views: 736

Answers (2)

lastcanal
lastcanal

Reputation: 2175

I assume that you want to call os:cmd without blocking your main process loop. In order to accomplish that you will need to call os:command from a spawned process and then send a message back to the Parent process indicating completion.

Here is an example:

runmatlab() ->
      receive
           updatemodel ->
               Parent = self(),
               spawn_link(fun() ->
                 Response = os:cmd("matlabscript.bat"),
                 Parent ! {updatedmodel, Response}
               end),
               runmatlab();

          {updatedmodel, Response} ->
              % do something with response
              runmatlab()
end.

Upvotes: 2

Oleksii Kachaiev
Oleksii Kachaiev

Reputation: 6232

For the first, Erlang process is something definitely different from os process. There is no "notifications" mechanism or "messaging" mechanism between them. What can you do is a) run new erlang node, b) connect to target node, c) send message to remote node.

But. Regarding to your question.

runmatlab() ->
      receive
           updatemodel->
               BatOutput = os:cmd("matlabscript.bat"),
               %% "here" BAT script has already finished
               %% and output can be found in BatOutput variable
...
end.

For the second, ports are about encoding/decoding erlang data type (in short words).

Upvotes: 0

Related Questions