3

I was trying to start node.js server on startup of the machine (ubuntu 16.04) with upstart by using the the following code in nodeserv.conf file:

#!upstart
description "Node.js server"
author "Sushant Kumar"

start on started mountall
stop on shutdown

respawn
respawn limit 99 5

script
    export APP_HOME = "/home/ubuntu/chatbot_server"
    export HOME = "/home/ubuntu"
    cd $APP_HOME
    exec sudo -u ubuntu /usr/local/nodejs/bin/node $HOME/chatbot_server/server.js >> /var/log/chatbotserv.log 2>&1
end script

post-start script
    echo "Node Started"
end script

, but I run the command

# start nodeserv

I get the followig error: >>start: Job failed to start. Can anyone help me please where I am going wrong?

Edit: This server is hosted on AWS EC2 instance (if that helps, I don't think it's relevant, but just in case).

Sushant Kumar
  • 91
  • 2
  • 5
  • Don't know about pm2, but i used forever-service but it could not succeed. I dont want to use any more modules/packages, just want to know why this script isn't working as expected. – Sushant Kumar Oct 24 '17 at 17:35

2 Answers2

4

Have a look on PM2.

PM2 is a really powerful Node.js process manager.

After install your app, you can easily set it on startup with:

sudo systemctl start pm2-yourusername
Lucas
  • 1,514
  • 3
  • 16
  • 23
3

You can do this by running your app as a service. You can use forever to ensure that a given script runs continuously. First of all you need to install forever. Then go to your project directory and install forever-monitor. Now you can start your app.

npm install forever -g
cd /path/to/your/project
npm install forever-monitor
forever start app.js

Now you need to use forever-service to build your node script as a service.Firstly, install forever-service and then install your app as a service.

npm install -g forever-service
forever-service install test

If you want to work on your script, you can replace this code in your script.

export HOME="/root"
exec /usr/local/nodejs/bin/node /home/ubuntu/chatbot_server/server.js >> /var/log/node.log 2>&1
Abdul Alim Shakir
  • 1,128
  • 13
  • 26