[erlang-questions] Erlang and Slurm sbatch scripts

zxq9 zxq9@REDACTED
Thu Sep 28 17:24:05 CEST 2017


On 2017年09月28日 木曜日 10:25:09 Miloud Eloumri wrote:
> Hello,
> 
> Is there an easy or automated way perhaps a tool or a package to write
> Slurm sbatch scripts to schedule and run Erlang module file or application
> on a server?
> 
> https://slurm.schedmd.com/sbatch.html
> 
> Can somebody please help me and provide an sbatch script (.sh) shows a way
> to schedule and run an Erlang module (.erl file) espically  a way to
> execute the file ?

Are you trying to execute a single file or a project residing in a directory
laid out the traditional way?

The simplest way of all would be to use an escript instead of an .erl file.
Escripts can start with a shebang (just like a shell script), have a main/1
function, and just execute right there.


  ceverett@REDACTED:~$ cat foo.escript 
  #! /usr/bin/env escript
  
  main(_) ->
      io:format("Reporting from within foo.escript.~n").
  ceverett@REDACTED:~$ chmod +x foo.escript 
  ceverett@REDACTED:~$ ./foo.escript 
  Reporting from within foo.escript.
  ceverett@REDACTED:~$


An .erl file would need to be compiled, loaded, and then called. The `make`
module has just the thing (assuming the file is in the current directory).
>From within Erlang:

  up_to_date = make:files(["foo.erl"]),
  foo:bar().

>From the shell it would be:


  ceverett@REDACTED:~$ cat foo.erl
  -module(foo).
  -export([bar/0]).
  
  bar() ->
      io:format("I am module ~tp~n", [?MODULE]),
      halt(0).
  ceverett@REDACTED:~$ erlc foo.erl
  ceverett@REDACTED:~$ erl -pa ./ -noshell -s foo bar
  I am module foo
  ceverett@REDACTED:~$


Note the explicit system halt called at the end of bar/0.
The invocation of the specific module and function name
are the arguments following `-s`.

Escripts are really the way to go for this, though.
If you DO have an .erl file that can't have a shebang at the
top, but you CAN force that it has a function called main/1,
then you can still run it as an escript:


  ceverett@REDACTED:~$ cat foo.erl
  -module(foo).
  -export([bar/0]).
  
  bar() ->
      io:format("I am module ~tp~n", [?MODULE]),
      halt(0).
  
  main(_) ->
      io:format("I didn't need to be exported!~n"),
      io:format("I also don't need to be explicitly terminated~n").
  
  ceverett@REDACTED:~$ escript foo.erl
  I didn't need to be exported!
  I also don't need to be explicitly terminated
  ceverett@REDACTED:~$


I don't know anything about sbatch, but hopefully this sort of points
you in the right direction. There are plenty of ways to skin this cat.

-Craig



More information about the erlang-questions mailing list