How to Use the Command 'erl' (with Examples)

How to Use the Command 'erl' (with Examples)

The erl command is an essential tool used to run and manage programs written in the Erlang programming language. Erlang is known for its ability to build scalable and fault-tolerant systems, often employed in telecommunication systems, messaging applications, and distributed computing. The erl command offers various functionalities including compiling and running Erlang scripts, connecting to nodes in distributed systems, and managing program execution.

Use Case 1: Compile and Run Sequential Erlang Program as a Common Script and Then Exit

Code:

erlc path/to/file1 path/to/file2 ... && erl -noshell 'mymodule:myfunction(arguments), init:stop().'

Motivation:

The motivation behind using this command is to automate the compilation and execution of Erlang scripts in a streamlined manner. This is especially beneficial for developers who want to keep their workflow efficient without manually entering commands to compile and then run their code. By compiling and running the program in a single command line, it accelerates development and testing cycles.

Explanation:

  • erlc path/to/file1 path/to/file2 ...: This part compiles your Erlang source files (.erl) into bytecode files (.beam) that can be executed by the Erlang runtime.

  • &&: Ensures that the Erlang program is executed only if the compilation succeeds, providing a safeguard against runtime errors due to compilation failures.

  • erl -noshell: Launches the Erlang virtual machine without the interactive shell, which is useful for automated script execution and non-interactive tasks.

  • 'mymodule:myfunction(arguments), init:stop().': Calls the specified function myfunction from the module mymodule with given arguments. The init:stop(). function gracefully stops the Erlang system after the script execution, freeing up resources.

Example Output:

After executing this command, you will see only the output from mymodule:myfunction(arguments), followed by a prompt return to the shell without any additional information from the Erlang shell environment.

Use Case 2: Connect to a Running Erlang Node

Code:

erl -remsh nodename@hostname -sname custom_shortname -hidden -setcookie cookie_of_remote_node

Motivation:

In distributed systems, processes running on different nodes often need to interact. Connecting to a running Erlang node enables developers and system administrators to perform monitoring, debugging, and management tasks in real-time across different machines. This capability is crucial in maintaining the health and performance of distributed applications.

Explanation:

  • -remsh nodename@hostname: Tells the erl command to connect to a remote shell on a specified node, facilitating interaction with the node’s running processes.

  • -sname custom_shortname: Sets a short name for the node you are connecting from, allowing quick identification and communication within its network segment.

  • -hidden: This option makes the connecting node not visible to other nodes except the one it’s directly connecting to, which can be used to prevent net_adm or net_kernel from exposing your node in node introspection calls.

  • -setcookie cookie_of_remote_node: Specifies the authentication token required for establishing connections with the remote node. This ensures secure communication between the nodes.

Example Output:

Once connected, you’ll get a remote terminal prompt similar to the local Erlang shell prompt. You’re now capable of executing commands on the remote node and viewing their outputs directly.

Use Case 3: Tell the Erlang Shell to Load Modules from a Directory

Code:

erl -pa path/to/directory_with_beam_files

Motivation:

During the development of Erlang applications, dynamically loading modules without specifying the full path each time saves time and effort. This command is particularly useful when working in environments where the application structure is modularized and based on different directories containing precompiled .beam files.

Explanation:

  • -pa path/to/directory_with_beam_files: This option tells the Erlang runtime to prepend the given directory to the code path, making all .beam files in that directory available for loading without additional specification.

Example Output:

Once executed, the Erlang shell will start, and you can call any functions from the modules located in the specified directory as if they were loaded in the default search path, enhancing productivity by simplifying module management.

Conclusion

The erl command is a powerful utility that provides diverse functionalities necessary for handling Erlang programs efficiently. Whether you’re streamlining your workflow by compiling and running scripts swiftly, connecting to remote nodes for real-time management of distributed systems, or managing module loading in a flexible manner, erl serves as a versatile tool in the Erlang developer’s toolbox. With the examples provided, you can leverage its capabilities to improve your development and deployment processes.

Related Posts

How to use the command 'bluetoothctl' (with examples)

How to use the command 'bluetoothctl' (with examples)

The bluetoothctl command is a powerful tool used to manage Bluetooth devices directly from the command line.

Read More
Mastering PDF Image Extraction with 'pdfimages' (with examples)

Mastering PDF Image Extraction with 'pdfimages' (with examples)

The pdfimages utility is an invaluable tool for anyone needing to extract images from PDF files efficiently and effectively.

Read More
How to Use the 'aws s3 rm' Command (with examples)

How to Use the 'aws s3 rm' Command (with examples)

The aws s3 rm command is a part of the AWS Command Line Interface (CLI) suite, enabling users to interact with Amazon S3, the widely-used object storage service.

Read More