Mahmoud Elgohary Mahmoud Elgohary - 3 months ago 15
Node.js Question

Run NodeJS as a Service on Ubuntu

i'm try to run nodejs as service as this steps
1-

sudo vim servicelocator.conf

2-
past this .conf


description "node.js server"
author "mahmoud elgohary"
# Used to Be: Start on Startup
# until we found some mounts weren't ready yet while booting:
start on started mountall
stop on shutdown

# Automatically Respawn:
respawn
respawn limit 99 5

script
# Not sure why $HOME is needed, but we found that it is:
export HOME="/root"

exec /usr/bin/node /var/lib/nodejsproject/servicelocator/server.js >> /var/log/node.log 2>&1
end script

post-start script
# Optionally put a script here that will notifiy you node has (re)started
# /root/bin/hoptoad.sh "node.js has started!"
end script


3-
init-checkconf /etc/init/servicelocator.conf


ERROR: File /etc/init/servicelocator.conf: syntax invalid: init:servicelocator.conf:1: Unknown stanza

4-

ubuntu@ip-172-31-37-243:/etc/init$ sudo start servicelocator
start: Unknown job: servicelocator

Answer

I did this with upstart:

  1. Install upstart sudo apt-get install upstart
  2. Create the .conf file to start you Node.js program sudo vim /etc/init/servicelocator.conf
  3. Edit the servicelocator.conf that you created and insert the text below:
#!upstart
description "servicelocator"

start on runlevel [2345] stop on runlevel [06]

#Automatically Respawn: respawn respawn limit 99 5

exec /usr/bin/node /var/lib/nodejsproject/servicelocator/server.js >> /var/log/node.log

Save and close the file.
To start the service: sudo start servicelocator

Comments