How to start node.js app on machine boot by creating a boot service
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
-
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
-
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
-
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
Related videos on Youtube
Admin
Updated on September 18, 2022Comments
-
Admin over 1 year
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?