Skip to content

Instantly share code, notes, and snippets.

@retrohacker
Created January 5, 2015 22:05
Show Gist options
  • Save retrohacker/be8fe66f1955faab13f3 to your computer and use it in GitHub Desktop.
Save retrohacker/be8fe66f1955faab13f3 to your computer and use it in GitHub Desktop.

Node.js Process Management:
Enterprise Linux 6

Learning Objectives

Debian Wheezy and other systems using the SysV style init with extend LSB Headers as defined in Chapter 20 of the LSB 3.1 specification


This unit will discuss how to approach a Node.js deployment using Wheezys SysV Init and associated tools

System V Init with LSB Headers

  • Old-style Init, configured with Bash scripts
  • Init script handles lifecycle commands
  • Extend specification to include process dependencies
  • Monitoring and auto-restart requires /etc/inittab or an external tool such as Monit
Find out how much the people in the room know about old-school init and the LSB extension. Skip the intro or adjust appropriately

Example Init Script

  • Starts the Node process
  • Directs stdout/stderr to a log file
  • Uses LSB functions and start-stop-daemon to manage process lifecycle

Example Init Script: Variables

file:nodejs:#1

Example Init Script: LSB Header

### BEGIN INIT INFO
# Provides:           {{SERVICE_NAME}}
# Required-Start:     $all
# Required-Stop:      $all
# Should-Start:
# Should-Stop:
# Default-Start:      2 3 4 5
# Default-Stop:       0 1 6
# Short-Description:  A Node Service
# Description:
# SysV style init script with LSB header. For use on Debian Wheezy.
### END INIT INFO

Example Init Script: LSB Functions

Wheezy ships with built-in functions that manage the lifecycle of a process and help ensure LSB 3.1 conformance.

# Import lsb functions
. /lib/lsb/init-functions

# Functions now available:
#   start_daemon [-f] [-n nicelevel] [-p pidfile] pathname [args...]
#   killproc [-p pidfile] pathname [signal]
#   pidofproc [-p pidfile] pathname
#   log_success_msg message
#   log_failure_msg message
#   log_warning_msg message

** NOT MINE FROM HERE DOWN **

Example Init Script:
Starting Instances Safely

fd exhaustion is covered in the resource management unit. init scripts are a good place to set ulimit if necessary.

Example Init Script:
Starting Instances Safely

file:nodejs-init.sh:#3

Example Init Script: Stopping

  • Send each instance a SIGTERM
  • After 10 seconds, send SIGKILL if still running

file:nodejs-init.sh:#5

Example Init Script:
Useful Utility Functions

file:nodejs-init.sh:#6

Monit

  • Used here to help ensure process stays alive
  • Can also monitor system resources such as file checksums and permissions
  • Can set process restart triggers, alert triggers
  • Works by polling

Monit Config File

  • Set a polling interval of one second
  • Check each node process by its pidfile
# daemonize and set a polling interval of 1 second
set daemon 1

check process nodejs1 with pidfile /var/run/nodejs.pid.1
  start program = "/etc/init.d/nodejs start"
  stop program = "/etc/init.d/nodejs stop"
  group nodejs

# ...repeat for each node instance

Summary

  • Enterprise Linux 5 uses the SysV Init system based on Bash script configuration
  • Init is only concerned with basic lifecycle commands, process monitoring is an external concern
  • Use system tools such as Monit to monitor and restart processes when they die
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment