There are several ways to manage and run a Node.js application which persists reboot/crash etc. One of easiest way we can use it using PM2.
PM2
PM2 is an advanced process manager for production Node.js applications.
Installing PM2
npm install -g pm2 # may require sudo
Starting the app
pm2 start ~/app/server.js
Note - If you are using ports less than 1000 then pm2
commands may require sudo privileges for this and all the following commands.
Saving the state
If we do not save, pm2 will forget the running apps on the next boot
pm2 save
Check status
pm2 list
Startup Daemon
PM2 can generate and configure a Startup Script to keep PM2 and your processes alive at every server restart.
Init Systems Supported: systemd
, upstart
, launchd
, rc.d
The following command generates Startup Script
pm2 startup
Remove Startup Script
pm2 unstartup