• This repository has been archived on 20/Apr/2023
  • Stars
    star
    133
  • Rank 272,600 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 13 years ago
  • Updated over 1 year ago

Reviews

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

Repository Details

Support for running node.js as a socket-activated service under systemd

⚠⚠⚠ No longer maintained! See @derhuerst/systemd for an alternative. ⚠⚠⚠


systemd for Node.js

Adds support for running node.js as a socket-activated service under systemd.

More info on the how and why: https://rocketeer.be/articles/deploying-node-js-with-systemd/

For more background on socket activation: http://0pointer.de/blog/projects/socket-activation.html

Obviously, this will only work on Linux distributions with systemd (such as Fedora).

Usage

You can install the latest version via npm:

$ npm install systemd

Require the systemd module and pass 'systemd' as a parameter to listen():

require('systemd');

var http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
}).listen('systemd');

Install a systemd socket file (e.g.: /etc/systemd/system/node-hello.socket):

[Socket]
ListenStream=1337

[Install]
WantedBy=sockets.target

Install a systemd service file (e.g.: /etc/systemd/system/node-hello.service):

# Adjust according to man 5 systemd.exec

[Service]
ExecStart=/path/to/bin/node /path/to/hello.js
StandardOutput=syslog
User=nobody
Group=nobody

Be sure to substitute the paths to node and your script!

  • ⚠ Run node directly or make sure your startup helper scripts can hand over the sockets. npm start probably won't work.

Reload the systemd daemon so that it picks up the new unit files:

$ systemctl --system daemon-reload

Enable and start the socket:

$ systemctl enable node-hello.socket
$ systemctl start node-hello.socket

Check the status of the socket:

$ systemctl status node-hello.socket
node-hello.socket
      Loaded: loaded (/etc/systemd/system/node-hello.socket)
      Active: active (listening) since Sat, 15 Oct 2011 20:27:47 +0200; 2s ago
      CGroup: name=systemd:/system/node-hello.socket

Great, it's running!

Check the status of the service, not running yet:

$ systemctl status node-hello.service
node-hello.service
      Loaded: loaded (/etc/systemd/system/node-hello.service)
      Active: inactive (dead)
      CGroup: name=systemd:/system/node-hello.service

Do a request to your service:

$ curl -i http://localhost:1337/
HTTP/1.1 200 OK
Content-Type: text/plain
Connection: keep-alive
Transfer-Encoding: chunked

Hello World

Check again, now it will be running:

$ systemctl status node-hello.service
node-hello.service
      Loaded: loaded (/etc/systemd/system/node-hello.service)
      Active: active (running) since Sat, 15 Oct 2011 20:32:10 +0200; 38s ago
    Main PID: 1159 (node)
      CGroup: name=systemd:/system/node-hello.service
              β”” 1159 /path/to/bin/node /path/to/hello.js

Only listen to systemd when running under systemd

You can make the systemd usage conditional by checking for the systemd environment variable:

var http = require('http');

require('systemd');

var port = process.env.LISTEN_PID > 0 ? 'systemd' : 1337;
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
}).listen(port);

This makes it possible to run the script stand-alone in development, yet use systemd when started through systemd.

Contributing

A jshint file is included to check code style.

Before submitting a pull request, please check your code. For convenience, I've also added a grunt file.

Install the dev dependencies:

$ npm install --dev

Install the grunt cli if you haven't already done so:

$ npm -g install grunt-cli

Run it:

$ grunt

License

(The MIT License)

Copyright (C) 2011-2014 by Ruben Vermeersch <[email protected]>

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

sql-migrate

SQL schema migration tool for Go.
Go
2,992
star
2

angular-gettext

Gettext support for Angular.js
JavaScript
649
star
3

grunt-git

Git commands for grunt.
JavaScript
227
star
4

point-in-svg-polygon

Determine if an point is inside a polygon
JavaScript
79
star
5

node-apk-parser

Extract Android Manifest info from an APK file.
JavaScript
76
star
6

pofile

Parse and serialize Gettext PO files.
JavaScript
64
star
7

angular-select2

Select2 directive for Angular.js
JavaScript
62
star
8

angular-gettext-example

TodoMVC with angular-gettext added to it.
JavaScript
46
star
9

angular-debounce

Tiny debouncing function for Angular.JS.
JavaScript
42
star
10

angular-gettext-tools

Tools for extracting/compiling angular-gettext strings.
JavaScript
39
star
11

grunt-angular-gettext

Tasks for extracting/compiling angular-gettext strings.
JavaScript
38
star
12

pgtest

PostgreSQL test helper for Go
Go
37
star
13

zipper

Insanely simple zipfile creator for node.js.
C++
33
star
14

broadcaster

Websocket server for broadcasting Redis pub/sub messages to web clients.
Go
33
star
15

node-autoquit

Automatically quit node.js servers when inactive.
JavaScript
33
star
16

node-xlsx-writer

Simple XLSX writer for Node.js.
CoffeeScript
29
star
17

angular-tiny-eventemitter

Tiny event emitter for Angular.JS.
JavaScript
24
star
18

angular-rt-popup

A better version of the Bootstrap popover, for Angular.JS
JavaScript
22
star
19

node-broadcast-hub

WebSockets backed by Redis pubsub.
CoffeeScript
18
star
20

angular-encode-uri

Encode URIs through a filter.
CoffeeScript
17
star
21

connect-body-rewrite

Rewrite request bodies.
JavaScript
16
star
22

grunt-mkdir

Create directories with Grunt.
JavaScript
16
star
23

modl-migrate

SQL Schema migration tool for Go, using modl.
Go
15
star
24

topojson

TopoJSON implementation in Go
Go
11
star
25

jupyter-octave

Jupyter lab with an Octave kernel
Dockerfile
10
star
26

dupefinder

Detect duplicate files across different machines
Go
9
star
27

pygmentize

Go wrapper for pygments
Go
9
star
28

mod_rewrite-to-nginx

Apache mod_rewrite rule to nginx rewrite rule convertor
JavaScript
9
star
29

docker-ttrss-plugins

Tiny Tiny RSS feed reader as a docker image, with plugins.
PHP
8
star
30

1pif-to-kdbx

Quick hack to export a 1pif (1Password export) to kdbx (KeePass).
Python
8
star
31

ec2-disable-source-dest

Disable the EC2 source/dest check from within an instance.
Go
8
star
32

concourse-sentry-releases-resource

Concourse Resource for Sentry releases
Shell
7
star
33

grunt-unknown-css

Detect undeclared CSS classes in your HTML.
CoffeeScript
5
star
34

tripod

Top secret Banshee-based photo app project! Playground for new ideas that will land in F-Spot.
C#
5
star
35

angular-optimistic-cache

Optimistically use cached data before a request finishes.
JavaScript
5
star
36

opencagedata

Go bindings for OpenCage Geocoder
Go
4
star
37

vimrc

VIM configuration
Vim Script
4
star
38

belgium-zipcodes

Zipcodes and NIS codes for Belgium.
CoffeeScript
4
star
39

angular-optimistic-model

Optimistically cached models for Angular.JS.
JavaScript
4
star
40

angular-async-series

Apply an action to a set of data serially.
JavaScript
4
star
41

ergast-mrd

Mirror of the Ergast.com database images
3
star
42

deploy-to

Git deployment utility script.
Shell
3
star
43

gorocksdb

gorocksdb is a Go wrapper for RocksDB, embedded by default
Go
3
star
44

kube-appdeploy

Go
3
star
45

nvimrc

NeoVIM configuration
Vim Script
2
star
46

broadcaster-client

JavaScript client for broadcaster (https://github.com/rubenv/broadcaster)
JavaScript
2
star
47

js-validate

JSHint wrapper + validation profiles
JavaScript
2
star
48

helm-cert-manager-issuers-letsencrypt

2
star
49

backbone_test

Toying around with Node.js and Backbone.js
JavaScript
2
star
50

fastyaml

Super fast YAML parser generator for Go
Go
1
star
51

osmtopo

OpenStreetMap topology extraction tools
Go
1
star
52

angular-resize

Window.resize hooks for Angular.JS.
JavaScript
1
star
53

goose

Database migration tool for Go.
Go
1
star
54

mapbox-dist

Compiled version of Mapbox.js.
JavaScript
1
star
55

deuac

Bootable ISO image to disable UAC on Windows machines.
Shell
1
star
56

mobile-monkeypatches

A collection of workarounds and hacks to make life a bit easier when developing mobile web apps.
JavaScript
1
star
57

gettext_sample

Sample showing a bug in the gettext for rails gem.
Ruby
1
star
58

rrpubsub

More reliable Redis PubSub for Go
Go
1
star
59

angular-import-scope

Import a scope from another ui-view.
JavaScript
1
star
60

stash-go-import

Go import path support for Atlassian Stash
Go
1
star
61

grunt-usemin-uglifynew

Usemin uglify task that reuses existing .min files.
JavaScript
1
star
62

query

Go SQL query builder
Go
1
star
63

connect-strip-manifest

Remove html5 app cache through connect middleware.
JavaScript
1
star
64

utf8mapper

Go
1
star
65

prometheus-jira-api-exporter

Jira exporter for Prometheus that uses the API to fetch data
Go
1
star