Mahmoud Elgohary Mahmoud Elgohary - 1 year ago 83
Node.js Question

Run NodeJS as a Service on Ubuntu

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

sudo vim servicelocator.conf

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 limit 99 5

# 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/ "node.js has started!"
end script

init-checkconf /etc/init/servicelocator.conf

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


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

Answer Source

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:
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