Home > English, Erlang > How to run easily your Erlang software (part II)

How to run easily your Erlang software (part II)

Here I am again!

Last time I told you how to run you code using escript, so today I will introduce you another easy way to handle the execution of your Erlang code.

Verba volant, “scripting” manent

In this post I will give you just an example on how you can run your code simply by using Unix commands, actually this method is really easy and I really like it, maybe because I run my first Erlang stuff in this way 🙂

As always let’s see an easy example of code:

$ less nomescript


export HEART_COMMAND="$EBIN/starting_script start"

case $1 in

    $ERL -sname mynode -pa $EBIN \
         -heart -detached -s my_module start_link

    echo "Usage: $0 {start|stop|debug}"
    exit 1

exit 0

Firts of all, this is a script, therefore it starts with the so called shebang, this way the rest of the file will be read as normal shell commands. After this we specify a variable named EBIN which indicates where the .beam files are stored and that will be used with the flag -pa in the erl command. The above mentioned flag adds a certain numbers of specified directories to the beginning of the code path.

The next line you will find is used to specify the path to the erlang version we want to use, actually I simply use it to reduce the quantity of text we are going to write next.

Let’s skip for now the export HEART_PART line, in fact we will see it later on this post, and let’s focus on the case sequence. In my script, I use the case to parse the argument given by the user to the script itself: if the argument is different from “start”, the script will provide a brief explanation on how to use the script (please notice that I left the “stop | debug ” stuff to show you that multiple choices can be handled. If the argument given to our script is “start” then something magic happens, what?

Well, in this case the erlang shell is called through the above described variable $ERL, actually we add a couple of flags to this call, therefore I will analyse them with you guys. First of all of course we have the above described flag -pa (we already know it), then we have -detached which is used to start the erlang runtime system detached from the system console (this is veeeeeeery useful for running daemons and backgrounds processes).

The flag -sname nodename transforms the Erlang runtime system into a distributed node, this flag is very close in use to -name, but the host name portion of the node name Name@Host will be the short name, not fully qualified.

The flag -s mod [fun [arg1, arg2, …]](init flag) or simply -s mod fun forces init to call the specified function, with the specific arguments. If you function is provided, it is assumed to be start. If no arguments are provided, the function is assumed to be of arity 0. Otherwise it is assumed to be of arity 1, taking the list [arg1,arg2,…] as argument. All arguments are passed as atoms.

You may notice that when we call erl we used the flag -heart (or -heart -env HEART_BEAT_TIMEOUT 30), this is because we want our Erlang runtime system to be monitored by the Heartbeat Monitoring of an Erlang Runtime System.

The module pointed by the previous link is useful because it sends periodic “heartbeats” to an external port program, which is called heart as well. Heart checks whether the Erlang runtime system it is supervising is still running or not. The port program listens for any heartbeats for a specific timeout called HEART_BEAT_TIMEOUT (default is 60 seconds but it can be between 10 and 65535), if no heartbeat is received in the aforesaid timeout the system is rebooted.

In case of rebooting, the environment variable HEART_COMMAND has to be set before the system is started. If this variable is not set, a warning text will be printed but the system will not reboot. (notice that HEART_COMMAND is exported in the 4th line of the script!)

To reboot on the WINDOWS platform HEART_COMMAND can be set to heart -shutdown (included in the Erlang delivery) or of course to any other suitable program which can activate a reboot.

Let’s see now how to run this code:

$ ./nomescript
Usage: nomescript {start|stop|debug}
$ ./nomescript start
Here it runs in background

Ok, hope I was clear….hope so! In case let me know!

Categories: English, Erlang
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: