Execute o Node.js como um serviço no Ubuntu

  • usar upstart
sudo apt-get install upstart
  • escrever script /etc/init/your_program.conf

exemplo

#!upstart
description
"node.js chatapp server"
author
"subicura"

start on startup

stop on shutdown


script

export HOME
="/home/hosting"

echo $$
> /var/run/chatapp.pid
exec sudo
-u hosting /usr/local/bin/node /hosting/nemoz/current/lib/nodejs/chatapp.js >> /var/log/chatapp.log 2>&1
end script


pre
-start script
# Date format same as (new Date()).toISOString() for consistency
echo
"[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /var/log/chatapp.log
end script


pre
-stop script
rm
/var/run/chatapp.pid
echo
"[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/chatapp.log
end script
  • iniciar / parar
/sbin/start your_program
/sbin/stop your_program