How to automatically get a nodejs service to start up after reboot

Description

Getting a service to start up after reboot is easy with Linux and crontab

In the example below, we have a Unix user called testuser. The requirement was that testuser wanted to run a nodejs script uisng forever.

We’ll be editing testuser‘s crons to include a startup command.

Example

Start editing by replacing the testuser with your desired runtime user for the Node process. If you choose a different user other than yourself, you will have to run this with sudo.

$ crontab -u testuser -e

If you have never done this before, it will ask you which editor you wish to edit with. I like vim, but will recommend nano for ease of use.

When working with CRONs, you should always use full paths when executing binaries. Use which forever to get the full path to forever. You will notice we have also included the full path to node.

Once in the editor add the following line:

@reboot /usr/local/bin/forever start -c /usr/local/bin/node /your/path/to/your/app.js

Save the file. You should get some feedback that the cron has been installed.

For further confirmation of the installation of the cron, execute the following (again replacing “testuser” with your target username) to list the currently installed crons:

$ crontab -u testuser -l

References

Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top