Skip to content

Instantly share code, notes, and snippets.

@3rd-Eden
Created May 10, 2011 08:18
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 3rd-Eden/964088 to your computer and use it in GitHub Desktop.
Save 3rd-Eden/964088 to your computer and use it in GitHub Desktop.
monit nodejs deployment
###############################################################################
## Includes
###############################################################################
##
## It is possible to include additional configuration parts from other files or
## directories.
#
# include /etc/monit.d/*
#
#
set logfile /var/log/monit.log
check host nodejs with address 127.0.0.1
start program = "/etc/init.d/nodestats start"
stop program = "/etc/init.d/nodestats stop"
if failed port 7331 protocol HTTP
request /ping
with timeout 10 seconds
then restart
#! /bin/sh
### BEGIN INIT INFO
# Provides: server.js
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts nodejs and server.js
# Description: starts nodejs and server.js using forever
### END INIT INFO
NODE_PATH=/usr/local/lib/node_modules
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/bin/forever
NAME=forever
DESC=nodestats
test -x $DAEMON || exit 0
set -e
case "$1" in
start)
echo -n "Starting $DESC: "
exports NODE_PATH=$NODE_PATH
$DAEMON start /www/OMG-ADD-YOUR-DIRECTORY-HERE-LAL/server.js
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
$DAEMON stop /www/OMG-ADD-YOUR-DIRECTORY-HERE-LAL/server.js
echo "$NAME."
;;
restart|force-reload)
echo -n "Stopping $DESC: "
$DAEMON stop /www/OMG-ADD-YOUR-DIRECTORY-HERE-LAL/server.js
echo "$NAME."
sleep 1
echo -n "Starting $DESC: "
exports NODE_PATH=$NODE_PATH
$DAEMON start /www/OMG-ADD-YOUR-DIRECTORY-HERE-LAL/server.js
echo "$NAME."
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
exit 1
;;
esac
exit 0
@3rd-Eden
Copy link
Author

Add the monitrc in your /etc/monit/monitrc file

Move the nodestats to /etc/init.d/nodestats.

And make sure your server.js has a ping hook. In this setup I just add a simple response to the ping requests using

case "/ping":
   res.writeHead( 200, {'Content-Type': 'text/plain'} );
   res.end( "pong" );
   break;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment