Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Run NodeJS as a Service on Ubuntu 14.04 LTS

Run NodeJS as a Service on Ubuntu 14.04 LTS

With Node you can write very fast JavaScript programs serverside. It's pretty easy to install Node, code your program, and run it. But > how do you make it run nicely in the background like a true server?

  • Go to /etc/init/
  • $ sudo vim yourapp.conf
  • Paste script.conf
  • $ sudo start yourapp
  • And when you wanna kill the process $ sudo stop yourapp

Reference

Thanks to [kvz.io] (http://kvz.io/blog/2009/12/15/run-nodejs-as-a-service-on-ubuntu-karmic/) for this!

description "node.js server"
author "Foo Bar"
# 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
export HOME="/root"
exec /usr//bin/nodejs /path/to/yourapp.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
@lintmouse

This comment has been minimized.

Copy link

lintmouse commented Jul 17, 2015

This did the trick. Thanks.

@AmaVic

This comment has been minimized.

Copy link

AmaVic commented Feb 12, 2016

Thanks a lot for this ! Really useful !

@iSaadSalman

This comment has been minimized.

Copy link

iSaadSalman commented Mar 10, 2016

Thank you!

@matt212

This comment has been minimized.

Copy link

matt212 commented Mar 14, 2016

how to daemon this service to run after server restart !
currently when I am executing in init.d folder
sudo update-rc.d service-name.sh defaults
the application does not starts after system reboots !

@sethleedy

This comment has been minimized.

Copy link

sethleedy commented Jun 12, 2016

Is "exec /usr//bin/nodejs" valid ? it has two //'s in sequence.

@dylanh724

This comment has been minimized.

Copy link

dylanh724 commented Jul 8, 2016

unknown job: myapp

@jiggy1com

This comment has been minimized.

Copy link

jiggy1com commented Jul 25, 2016

looks too easy! haha can't wait to try it, thx! ... and 3 hours later, voila! It worked, but I had to reconfigure some constants to work with nodemon (local dev) and this script (prod). I had some paths set with fs.realpathSync, then tried to use __dirname but ended up having to use path.resolve(__dirname, '..') [+ '/whatever'] -- Just putting it out there in case anyone else runs into similar issues. (I'm new to NodeJS if you couldn't tell, but stoked on it)

@cdvillagra

This comment has been minimized.

Copy link

cdvillagra commented Nov 22, 2016

Good job Samuel!!

@Qurbonov

This comment has been minimized.

Copy link

Qurbonov commented Jan 27, 2017

how to start :)
root@debian:/etc/init# start myapp
bash: start: command not found

@dharm6619

This comment has been minimized.

Copy link

dharm6619 commented Jun 13, 2017

start: Unable to connect to Upstart: Failed to connect to socket /com/ubuntu/upstart: Connection refused

This was the error showed to me ... Help me out

@akemando

This comment has been minimized.

Copy link

akemando commented Jun 22, 2017

Had the same error.-
I ran below instructions on Ubuntu 16.04.1 LTS and works for me.

sudo apt-get install upstart-sysv
sudo update-initramfs -u
sudo reboot

here's the link
https://askubuntu.com/questions/614970/vivid-failed-to-connect-to-upstart-connection-refused

:)

@jchungkr

This comment has been minimized.

Copy link

jchungkr commented Jul 1, 2017

@sethleedy

Is "exec /usr//bin/nodejs" valid ? it has two //'s in sequence.

apparently this is fine (I tried cd-ing into both '/usr/bin' and also '/usr//bin' directories and both resolve)
here is a link to superuser.com

now I just have to figure out how to get this to open my script in a screen

@lokeshj3

This comment has been minimized.

Copy link

lokeshj3 commented Jul 14, 2017

I did it by pm2 this way(Ubuntu 16.04.01 LTS):
npm install -g pm2
pm2 start yourapp.js
pm2 startup
pm2 save

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.