• Stars
    star
    419
  • Rank 103,397 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 11 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Create native background daemons on Linux systems.

node-linux

NPM version NGN Dependencies Build

Sponsors (as of 2020)

Follow the author on Twitter (@goldglovecb).

Contributions Requested

(see below)

Documentation is available at the node-linux portal.

This is a standalone module, originally designed for internal use in NGN. However; it is capable of providing the same features for Node.JS scripts independently of NGN.

For alternative versions, see node-windows and node-mac

This module makes it possible to daemonize Node.js scripts natively (using systemv init.d scripts).

To start, install node-linux via:

npm install node-linux

node-linux has a utility to run Node.js scripts as Linux daemons.

To create a service with node-linux, prepare a script like:

  var Service = require('node-linux').Service;

  // Create a new service object
  var svc = new Service({
    name:'Hello World',
    description: 'The nodejs.org example web server.',
    script: '/path/to/helloworld.js'
  });

  // Listen for the "install" event, which indicates the
  // process is available as a service.
  svc.on('install',function(){
    svc.start();
  });

  svc.install();

The code above creates a new Service object, providing a pretty name and description. The script attribute identifies the Node.js script that should run as a service. Upon running this, the script will be available to the system. By default, node-linux produces systemv init scripts, meaning the services can be managed by typing service myapp start or service myapp stop (or service myapp status in some cases).

Windows Mac

The Service object emits the following events:

  • install - Fired when the script is installed as a service.
  • alreadyinstalled - Fired if the script is already known to be a service.
  • invalidinstallation - Fired if an installation is detected but missing required files.
  • uninstall - Fired when an uninstallation is complete.
  • start - Fired when the new service is started.
  • stop - Fired when the service is stopped.
  • error - Fired in some instances when an error occurs.
  • doesnotexist - Fired when an attempt to start a non-existent service is detected.

In the example above, the script listens for the install event. Since this event is fired when a service installation is complete, it is safe to start the service.

Services created by node-linux are like other services running on Linux. They can be started/stopped using service myapp start or service myapp stop and logs are available (default is in /var/log).

Environment Variables

Sometimes you may want to provide a service with static data, passed in on creation of the service. You can do this by setting environment variables in the service config, as shown below:

  var svc = new Service({
    name:'Hello World',
    description: 'The nodejs.org example web server.',
    script: '/path/to/helloworld.js',
    env: {
      name: "HOME",
      value: process.env["USERPROFILE"] // service is now able to access the user who created its' home directory
    }
  });

You can also supply an array to set multiple environment variables:

  var svc = new Service({
    name:'Hello World',
    description: 'The nodejs.org example web server.',
    script: '/path/to/helloworld.js',
    env: [{
      name: "HOME",
      value: process.env["USERPROFILE"] // service is now able to access the user who created its' home directory
    },
    {
      name: "TEMP",
      value: path.join(process.env["USERPROFILE"],"/temp") // use a temp directory in user's home directory
    }]
  });

Setting run as user/group

By default your node service will run as root:root. You may not want that. Just pass the requested user/group values at startup

  var svc = new Service({
    name:'Hello World',
    description: 'The nodejs.org example web server.',
    script: '/path/to/helloworld.js',
    user: "vagrant",
    group: "vagrant"
  });

Cleaning Up: Uninstall a Service

Uninstalling a previously created service is syntactically similar to installation.

  var Service = require('node-linux').Service;

  // Create a new service object
  var svc = new Service({
    name:'Hello World',
    script: require('path').join(__dirname,'helloworld.js')
  });

  // Listen for the "uninstall" event so we know when it's done.
  svc.on('uninstall',function(){
    console.log('Uninstall complete.');
    console.log('The service exists: ',svc.exists());
  });

  // Uninstall the service.
  svc.uninstall();

The uninstall process only removes process-specific files. It does NOT delete your Node.js script, but it will remove the logs!

What Makes node-linux Services Unique?

Lots of things!

Long Running Processes & Monitoring:

There is no built-in service recovery in most Linux environments, and third party products can be fairly limited or not easily configured from code. Therefore, node-linux creates a wrapper around the Node.js script. This wrapper is responsible for restarting a failed service in an intelligent and configurable manner. For example, if your script crashes due to an unknown error, node-linux will attempt to restart it. By default, this occurs every second. However; if the script has a fatal flaw that makes it crash repeatedly, it adds unnecessary overhead to the system. node-linux handles this by increasing the time interval between restarts and capping the maximum number of restarts.

Smarter Restarts That Won't Pummel Your Server:

Using the default settings, node-linux adds 25% to the wait interval each time it needs to restart the script. With the default setting (1 second), the first restart attempt occurs after one second. The second occurs after 1.25 seconds. The third after 1.56 seconds (1.25 increased by 25%) and so on. Both the initial wait time and the growth rate are configuration options that can be passed to a new Service. For example:

  var svc = new Service({
    name:'Hello World',
    description: 'The nodejs.org example web server.',
    script: '/path/to/helloworld.js'),
    wait: 2,
    grow: .5
  });

In this example, the wait period will start at 2 seconds and increase by 50%. So, the second attempt would be 3 seconds later while the fourth would be 4.5 seconds later.

Don't DOS Yourself!

Repetitive recycling could potentially go on forever with a bad script. To handle these situations, node-linux supports two kinds of caps. Using maxRetries will cap the maximum number of restart attempts. By default, this is unlimited. Setting it to 3 would tell the process to no longer restart a process after it has failed 3 times. Another option is maxRestarts, which caps the number of restarts attempted within 60 seconds. For example, if this is set to 3 (the default) and the process crashes/restarts repeatedly, node-linux will cease restart attempts after the 3rd cycle in a 60 second window. Both of these configuration options can be set, just like wait or grow.

Finally, an attribute called abortOnError can be set to true if you want your script to not restart at all when it exits with an error.

How Services Are Made

node-linux uses the templates to generate init.d scripts for each Node.js script deployed as a service. This file is created in /etc/init.d by default. Additionally, a log file is generated in /var/log/<name> for general output and error logging.

Event Logging

A log source named myappname.log provides basic logging for the service. It can be used to see when the entire service starts/stops.

By default, any console.log, console.warn, console.error or other output will be made available in one of these two files.

Contributions

Due to some unforeseen life circumstances, I was not able to add all of the features I'd hoped to add before releasing this. I'll chip away at them over time, but I would be very interested in community contributions in the following areas:

  • systemd script generation
  • upstart script generation

I have also added a tag in the issues called feature request to keep a running to-do list.

If you are interested in working on one of these features, please get in touch with me before you start to discuss the feature.

License (MIT)

Copyright (c) 2013 Corey Butler

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

nvm-windows

A node.js version management utility for Windows. Ironically written in Go.
Go
36,739
star
2

node-windows

Windows support for Node.JS scripts (daemons, eventlog, UAC, etc).
JavaScript
2,799
star
3

fenix

A simple and visual static web server with collaboration features.
JavaScript
1,649
star
4

node-mac

Node utilities for Mac
JavaScript
536
star
5

iam

Identification and access management library for all JS runtimes that support ES Modules.
JavaScript
78
star
6

porthog

Identify which process is using a specific port.
JavaScript
27
star
7

fenix-cli

A command line interface for Fenix web servers.
JavaScript
26
star
8

localenvironment

Simply and optionally apply environment variables if they exist in env.json.
JavaScript
22
star
9

nodedistro

A node.js distribution metadata service, designed to support node version managers.
22
star
10

shortbus

A lightweight flow control module that works like a mini event bus + serial/parallel processor.
JavaScript
20
star
11

cfamazon

ColdFusion Amazon Integration
ColdFusion
17
star
12

JSAppStack

Code for the "Future of JS App Architecture" Sencha Meetup on 9/6/2011.
JavaScript
14
star
13

cfw

Cloudflare worker local development environment and publishing tool.
JavaScript
11
star
14

common-api

A collection of utility methods for producing API's.
JavaScript
11
star
15

musthave

A Node.js helper module for checking object elements against a list of required elements.
JavaScript
10
star
16

go-fsutil

A light abstraction for common file system functions.
Go
10
star
17

go-timer

A timer library, designed for porting JavaScript's setTimeout and setInterval functions to Go.
Go
8
star
18

tappedout

A simple "back to basics" JavaScript test runner for producing TAP-formatted results. Built using ES Modules.
JavaScript
8
star
19

productionline

An extendable class for creating builds (transpilation, minification, concatenation, etc).
JavaScript
6
star
20

sslgen

Quickly generate self-signed SSL certificates for testing.
JavaScript
6
star
21

clifirst

The demo app for my OpenJS World 2020 Conference talk: "The Benefits of a 'CLI First' Development Strategy"
6
star
22

go-where

Determines where a global executable exists on a computer.
Go
6
star
23

daemon-manager

A utility module designed to standardize the daemon management API across node-windows, node-mac, and node-linux.
JavaScript
6
star
24

goose

Simple MongoDB Organizational Pattern
JavaScript
5
star
25

productionline-web

An extension of productionline, providing common web/UI build capabilities.
JavaScript
5
star
26

tracktime

A cross-runtime "stopwatch" for JavaScript environments. Use it to track execution times.
JavaScript
5
star
27

workspace

A container-based workflow management utility.
JavaScript
4
star
28

assettracker

AssetTracker is a web-based tool for documenting assets in your IT infrastructure.
ColdFusion
4
star
29

servers

Server scripts
Shell
4
star
30

github-autotag

Monitor package.json for version changes and auto-tag new releases.
JavaScript
3
star
31

go-localenvironment

Apply environment variables if they exist in env.json.
Go
3
star
32

postit

A visual survey building tool built on ColdFusion and Ext JS
JavaScript
3
star
33

staticassets

A repository for blog assets.
3
star
34

tapfmt

A standalone TAP formatter application for all operating systems and runtimes.
Go
2
star
35

dev-cdn

A repository of static assets.
2
star
36

customlog

Really simple custom logging.
JavaScript
2
star
37

bugconnect

BugConnect is a small package of CF components that connect to and interact with Bugzilla.
ColdFusion
2
star
38

site-cb

CB Site
CSS
1
star
39

gulp-depalert

A gulp plugin to identify outdated dependencies.
JavaScript
1
star
40

promo

Assets for promoting projects, sponsorship, etc.
1
star
41

aoi

JavaScript
1
star
42

polymer-doc-generator

Generate JSON representations of custom Polymer-based web components. Also provides an example static HTML doc builder utility.
JavaScript
1
star
43

quickimage

An image conversion library.
JavaScript
1
star
44

cms

Static CMS UI
1
star
45

coreybutler

About Corey
1
star
46

etl-shell

A base app for building ETL processes with JavaScript
1
star
47

bg

Run and deploy Node.js scripts as background services.
JavaScript
1
star