How do I start erlang with initial command?

Hi Erlangers,

At the moment I have a shell script that I call to start the Erlang shell and bootstrap my Nitrogen application.

/bin/nitrogen console

However, I want to make it, so after my application is started it additionally runs

sync:go().

Is there any way to do this automatically from a shell script?

2 Likes

Hi,

I have been waiting a few days for an answer. Is there a way to pass Erlang commands from a shell script or am i out of luck?

1 Like

I’m not sure I completely understand, but I can think of two things that might help:

  • erl has a way to run functions by giving it Module/Function/Arguments on the command line. Look at the -s option. For example, I have a little module that runs the built-in web server by providing a bunch of parameters for me, and I typically run it from the command line like this:
    erl -detached -s webserver start where -detached means to run the resulting Erlang node in the background detached from the terminal (so I can log out and it will still be running), and -s webserver start runs webserver:start() (with no arguments).
  • You could run an escript that has any Erlang code in it you like. See Erlang -- escript to get started. I use escripts a lot, for example, when I want to give students a command they can run that will then interact with Erlang services I have running.

I hope this helps!

3 Likes

Essentially I want to have 2 shell scripts:

cons.sh
bin/nitrogen console

This will start the nitrogen server which I have built my app with and open the Erlang console.

ws.sh
<your answer here>

Same as above but, will additionally run the Erlang function go/0 in the sync module after the initial initiation is finished. This will check the file system for any changes in the files of the running app and recompile them when needed. This is useful during development.

Hope this helps!

1 Like

I have never used Nitrogen, but this is an easy one using Zotonic.
Check the developer guider.
The mod_development says this:

Automatic recompilation
The core Zotonic system starts either inotify-tools or fswatch, depending on which one is available. You have to install one of these to enable auto-compile and auto-load of changed files.

Also, addsite command has the app option:

If true, initializes a site app and a root supervisor when the site starts (default: false)

This allows any custom code to run when the site starts.

My comment is just FYI. I think someone will help you with Nitrogen.

2 Likes