0

My machine is Ubuntu 16.04. I want to start my node.js application when the machine boots up everytime. According to this post about autostarting services. I tried to make a service by doing the following:

1) I created a script to start node app with node's forever module.

#!/bin/bash

echo "Starting App"

forever -a start /opt/app/app.js

echo "App started"

2) I named this script startApp.sh and put this script inside /etc/init.d/ folder.

3) I ran the command update-rc.d startApp defaults

But I got the error update-rc.d: error: initscript does not exist: /etc/init.d/startApp

What did I do wrong?

2

There are multiple ways to achieve this PM2 You can use PM2 which provides you to create a startup script to start up for expected or unexpected machine restarts.

http://pm2.keymetrics.io/docs/usage/startup/

Docker I prefer to create Dockerfile and run it with --restart=always tag

If you want to continue with your shell script Then use unix's crontab It's very easy to use & you can configure in minutes

  1. Edit crontab run the following replacing the "USER" 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 USER -e

  2. Once in the editor add the following line:

    @reboot /usr/local/bin/forever start /your/path/to/your/index.js else

    @reboot sh /your/path/to/your/startApp.sh

  3. Save & confirm file is saved by check command of #1 again

    Note: In my opinion, you should use the full path in crontab file to prevent issues

You can refer this URL reference Ubuntu Cron HowTo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.