Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start node server on machine startup

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).

like image 746
Sushant Kumar Avatar asked Oct 22 '25 04:10

Sushant Kumar


2 Answers

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
like image 115
Abdul Alim Shakir Avatar answered Oct 23 '25 19:10

Abdul Alim Shakir


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
like image 27
Lucas Avatar answered Oct 23 '25 18:10

Lucas