Auto Restart Forever (NodeJS) on Ubuntu 14

A simple way to ensure that the forever module restarts your app, at least on ubuntu 14, is to use the  @reboot option in cron.

Edit cron with crontab -e

It is a good idea to get familiar with how to use cron. But this post is a cut-to-the-chase here’s how to do it explanation, so here goes.

Log into your remote ubuntu 14 server as the user that has permission to use the forever application.

ssh youruser@174.76.19.39

Use crontab with the -e option to edit the crontab document.

ubuntu$>crontab -e

Add this line:

@reboot /usr/local/bin/forever start -c /usr/bin/nodejs -l forever.log -a -o out.log -e err.log path/to/yourApp.js

NOTE: Its important to note that the path to the app in this case is relative to the home directory of the user used to start the app.

Then save the file.

Notice that the -a flag is used. This is because you may get an error if the forever.log exists (and likely would). The -a flag indicates to append the forever.log file. Since forever is happy to append, it no longer complains. Not to mention you retain your log file from before the system restart.

Now you can test this out by restarting your system. If Forever comes back, you did it!

Leave a Reply

Your email address will not be published.