Search code examples
node.jsembedded-linuxforever

Running forever for NodeJs from rc.local


I want NodeJs server to start at boot and be sure it stays up if it crashes with forever.

I've read a lot of posts on how to do it and the possible issues associated with it. The problem is that I have a limited memory available on given machine so I'm forced to "contain" NodeJs.

Reading the forever documentation I found out that it can be done using the command:

forever start -c "node --max_old_space_size=512" myapp.js

In this way forever calls the node server passing the memory argument which, in this case, limits the heap to 512 MB. So I wrote my sh script for rc.local like this:

#!/bin/sh
cd /forever_bin_dir
./forever start -c "node --max_old_space_size=512" /myapp_dir/myapp.js

Since rc.local works with limited environmental variables, "node" can't be find and the script fails.

I even tried with Cron adding to its file the line:

@reboot /forever_bin_dir/forever start -c "node --max_old_space_size=512" /myapp_dir/myapp.js

But the same issue persist.

My question is: how can i run forever at boot time passing at the same time the argument about memory ?


Solution

  • The best way I found It's just setting the PATH inside a script.

    I created a sh script in the home folder called launchforever.sh I got the environmental variables using the #printenv command.

    The launchforever script is something like this:

    #!/bin/sh
    export PATH=<copied from printenv>:$PATH
    forever start -c "node --max_old_space_size=30" > /dev/null
    

    After saving I gave it the permissions:

    #chmod 700 /home/<user>/launchforever.sh
    

    Then I added it to Cron:

    #crontab -u <user> -e
    

    And inside its file I added:

    @reboot /home/<user>/launchforever.sh