2

I have always started nitrogen to run as a daemon using the command below:

sudo /home/someuser/myapp/bin/nitrogen start

It works well but i have to repeat the same activity should the server reboot.

Most web servers by default start at boot time. When nitrogen is started, it starts the underlying Erlang web server. Unfortunately I have not found any single resource talking about starting nitrogen at boot time.

How do you start nitrogen as a daemon at system boot time?

Steve Vinoski
  • 19,847
  • 3
  • 31
  • 46

1 Answers1

2

The easiest solution is to use the /etc/rc.local file. By default, it's empty.

Since rc.local runs as root, you can use it as such (though if you prefer to run Nitrogen as a separate user, using su -c "command" username) is good.

Anyway, the simple solution is to add to your rc.local file the following:

To run as root:

/home/someuser/myapp/bin/nitrogen start

To run as another user:

su -c "/home/someuser/myapp/bin/nitrogen start" someuser

That will launch Nitrogen appropriately and will let you connect to the VM using bin/nitrogen attach.

My previous recommendation of using sudo is not sufficient, as it doesn't reset the environment to the user you want.

I'm using this in production on Ubuntu 14.04 and a linode VPS.

I hope that helps.

chops
  • 2,572
  • 1
  • 16
  • 25
  • I am afraid your solution looks straight forward but I do not know whats I am doing wrong. I have tried both options and they have failed to work. Do i have to carry out any other changes to the rc.local file to work? – Vianney Sserwanga Sep 29 '15 at 08:57
  • I'm curious what part isn't working: Does the Erlang VM Start up at all? Or does it start up, but is not responding to connections? Can you attach to the VM with `bin/nitrogen attach`? If you do a `ps aux | grep nitrogen` does anything come up? – chops Sep 29 '15 at 16:16
  • I realize that in my original answer, I completely forgot to mention the location of rc.local must be `/etc/rc.local`, just in case that was not clear – chops Sep 29 '15 at 16:17
  • Any luck since last posting? – chops Oct 04 '15 at 03:39
  • Well the sudo -u option worked on my laptop but after preceeding the statement with 'sleep 15'. I tried it on the virtual server and it did not work. – Vianney Sserwanga Oct 07 '15 at 08:39
  • Interesting. It works just fine on my local VM. Is this running on a local VM, or something more like a Linode/Digital Ocean/Cloud-whatever VM? Are you able to attach to the running VM, or does it show in `ps aux`? – chops Oct 07 '15 at 19:07
  • I am running it on Linode VM. On my local VM it runs after preceding with `sleep 15` but i failed to attach to it successfully. Yes it does show in `ps aux | grep nitrogen` – Vianney Sserwanga Oct 09 '15 at 12:46
  • Verrrry interesting. I'll play with Linode and see what I can come up with. – chops Oct 09 '15 at 19:33
  • I've been doing some further research on this, and I think I'll have a script ready for you to try soon. – chops Mar 24 '16 at 04:30
  • Looking up to you for a lasting solution. – Vianney Sserwanga Mar 26 '16 at 07:46
  • Alrighty, here's the dumb solution I came up with that works for me on 14.04 and a linode. Add to your rc.local file: `su -c "/path/to/nitrogen_app/bin/nitrogen start" desired_username` I'm updating my answer in a moment. – chops Mar 27 '16 at 04:58
  • I am afraid I do not understand why it does not work! – Vianney Sserwanga Mar 30 '16 at 20:38
  • Weird. I'm not sure. It's working for me. I made one change to the `bin/nitrogen` script (https://github.com/nitrogen/nitrogen/commit/fd48133ed91cec9d0b9e751fcea9172b0a00d984), but that change was because I have many applications running on a single machine, each under a different username from their respective `home` directory. – chops Mar 31 '16 at 13:39
  • What you could try is to scatter some `logger` messages throughout the `nitrogen` script (like this: https://github.com/nitrogen/nitrogen/blob/master/rel/overlay/common/bin/nitrogen#L291) to see what's happening. You can then see the results in the syslog (`/var/log/syslog`). – chops Mar 31 '16 at 13:44
  • This approach at times but no always starts nitrogen but what I realized it does not load some of the application dependence modules making the application unable to work at boot time. I am afraid at times it does not start at all! So I depend on backup servers in case the main servers go off without warning. – Vianney Sserwanga May 17 '16 at 14:54
  • It doesn't always load the dependency Erlang applications, or do you mean other non-Erlang applications that your app depends on (like, say a database server)? – chops May 17 '16 at 20:50
  • To be specific it does not load the **protocol buffer** _riak-erlang-client_ linking the nitrogen framework to the Riak database. The client is included in the framework through the file **rebar.conf**. – Vianney Sserwanga May 18 '16 at 03:01
  • I realized the reason why the application fails to work after nitrogen starting at boot time is because Mnesia database which is part of the application fails to normally. With **FATAL** mnesia_tm crashed - cannot read the schema report – Vianney Sserwanga May 18 '16 at 03:18
  • Interesting. That definitely gives you a bit of insight into what's going on. I've not used mnesia enough to know how to fix that problem, but that certainly sounds like it's giving you some decent hints enough so to fix it. – chops May 18 '16 at 05:29