keep alive webserver

254 views
Skip to first unread message

Flavio Del Bianco

unread,
Mar 12, 2012, 11:02:20 AM3/12/12
to nodejs
Hi, I'm learning nodejs and I had a doubt in how I keep alive a
webserver without running it everytime like $ node myserver.js? and
how it can connect alone every time the server get on like it does
apache.

Tim Caswell

unread,
Mar 12, 2012, 12:08:54 PM3/12/12
to nod...@googlegroups.com
it depends on your system.  On Ubuntu I use upstart and create a small script in /etc/init/SOMENAME.conf.  And then you can use start, stop, and restart like  `sudo start SOMENAME`

Here is the upstart config for howtonode.org:

    description "Run node server"

    env PATH=/home/tim/nvm/v0.6.11/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

    respawn
    start on runlevel [23]

    script
      cd /home/tim
      exec node server.js > access.log 2>> error.log 
    end script

Because of the "start on runlevel [23]" line it's started whenever the server reboots.  The "respawn" line restarts the node process if it dies.


--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

theCole

unread,
Mar 12, 2012, 4:56:33 PM3/12/12
to nod...@googlegroups.com
Tim, 

Do you have the link to the blog where that was posted?

-Cole

theCole

unread,
Mar 12, 2012, 4:56:53 PM3/12/12
to nod...@googlegroups.com
( the actual post )

Mikeal Rogers

unread,
Mar 12, 2012, 4:59:12 PM3/12/12
to nod...@googlegroups.com
I also use upstart. I *hate* upstart, but keeping your process up is something you want the kernel to be responsible for.

Tim Caswell

unread,
Mar 12, 2012, 5:11:16 PM3/12/12
to nod...@googlegroups.com
There is an old article on howtonode written by Tim Smart I think, but it's not what I use.  If you want to use upstart, best to read the manual.

--

nan huo

unread,
Mar 13, 2012, 5:08:12 AM3/13/12
to nod...@googlegroups.com
forever is more easier to start with https://github.com/nodejitsu/forever

Anand George

unread,
Mar 13, 2012, 5:20:52 AM3/13/12
to nod...@googlegroups.com
Here's the article if that helps.

--

Flavio Del Bianco

unread,
Mar 15, 2012, 5:43:15 AM3/15/12
to nodejs
Ok once I createed the file /etc/init/mynode.conf what I have to do? I
tryed immediatly to make "sudo start mynode" but server returns me :
start: Unknown job: mynode
What this it means?

g sambolic

unread,
Mar 15, 2012, 10:00:45 AM3/15/12
to nod...@googlegroups.com

Flavio Del Bianco

unread,
Mar 15, 2012, 11:06:08 AM3/15/12
to nod...@googlegroups.com
I can't install it for problems with versions. I tryed but I get errors after written the command
npm install forever

Sirwan Qutbi

unread,
Mar 17, 2012, 1:22:44 PM3/17/12
to nod...@googlegroups.com
Don't use Forever .. load of crap.

It only works with old versions.

Dave Clements

unread,
Mar 17, 2012, 9:02:04 PM3/17/12
to nod...@googlegroups.com
they updated...

Fadrizul H.

unread,
Mar 18, 2012, 12:01:06 AM3/18/12
to nod...@googlegroups.com
Yeah man, they updated like months ago =.=
Sent from my BlackBerry®

From: Sirwan Qutbi <sirwa...@gmail.com>
Date: Sat, 17 Mar 2012 10:22:44 -0700 (PDT)
Subject: [nodejs] Re: keep alive webserver
--

Nico Kaiser

unread,
Mar 19, 2012, 10:15:50 AM3/19/12
to nod...@googlegroups.com
Well, forever does not help when you not only need something that restarts the script on error, but a real startup script (which starts the node service when the server reboots).

I wrote a simple init script for this purpose (with logging and user switching) for sysvinit: https://github.com/nicokaiser/node-init

Nico

HG

unread,
Mar 20, 2012, 8:29:11 AM3/20/12
to nod...@googlegroups.com
Hi!

I was just down this same road. This is the upstart script that I ended up with on my system (RH 6.1):
#!upstart
description "NodeApp - node.js HTTP-server"
author      "Me"

# One example said that it's safer to start after mounts
#start on startup
start on started mountall
stop on shutdown

# respawn
respawn
# If the process is respawed more than COUNT times within an interval of INTERVAL seconds the process will be stopped and not restarted
# respawn limit COUNT INTERVAL
respawn limit 10 2

# settings
env APP_NAME="NodeApp"
env APP_DIR="/home/nodeuser/dev/NodeApp"
env APP_FILE="NodeApp.js"
env RUN_AS="nodeuser"
# This is setting for the app...
env NODE_ENV="production"

script
    export HOME="/home/${RUN_AS}/"
    export NODE_ENV

    echo $$ > /var/run/${APP_NAME}.pid
    # sudo doesn't work on redhat 6.1
    # exec sudo -u nodeuser /usr/local/bin/node /home/nodeuser/dev/NodeApp.js >> /var/log/NodeApp.log 2>&1
    exec su --session-command="/usr/local/bin/node ${APP_DIR}/${APP_FILE}" >> /var/log/${APP_NAME}.log 2>&1 ${RUN_AS}

    # root works also - but you don't want to run as root, right
    # echo $$ > /var/run/NodeApp.pid
    # export HOME="/root"
    # exec /usr/local/bin/node /home/nodeuser/dev/NodeApp/NodeApp.js >> /var/log/NodeApp.log 2>&1
end script

pre-start script
    echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /var/log/${APP_NAME}.log
end script

pre-stop script
    rm /var/run/${APP_NAME}.pid
    echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/${APP_NAME}.log
end script

Start with 'sudo start NodeApp' and stop with sudo stop... When you develop this, do not enable respawn until it works otherwise. You can run it as root if you want (if you need port 80), but otherwise, I think it's much better to run as some other user. Yes, it could be cleaner, but I'll hope you get the important points.

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en



--
HG.

George Snelling

unread,
Apr 3, 2012, 11:24:07 PM4/3/12
to nod...@googlegroups.com
The following isn't for mission-critical systems, but it's been working so well for us and is so simple that I thought I'd share.  We use nodemon (https://github.com/remy/nodemon) to start our node server.  It's just file watcher that restarts node if a source file changes, normally used for dev.  It is much smaller and has fewer moving parts than forever.  node-dev is a similar tool (https://github.com/fgnass/node-dev) that many people like that does basically the same thing.    

our startup script is something like this:  

nohup nodemon <app> >>/var/log/app.log 2>&1  & 

Then in our main app startup function, we have this charming little hack:  

process.on('uncaughtException', function(err) {
  console.error(err.stack||err)
  if (serverStarted) {     
    fs.writeFileSync('./crash.js', '// App crashed ' + Date() + '\n\n' + err.stack)
  }
  process.exit(1)
})


The vast majority of our server crashes are due to bugs in our code, not external problems like hardware, and this works fine for those.  If my job were on the line to keep the server alive no matter what I would figure out upstart or monit, but they both make my head hurt, and for the 90% case, this does the job.

Cheers!  

-George
Reply all
Reply to author
Forward
0 new messages