Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.

[doc][readme] hint: run node directly #14

Merged
merged 2 commits into from
Nov 28, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 97 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,112 +3,139 @@
Adds support for running node.js as a socket-activated service under systemd.

More info on the how and why: http://savanne.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).

Developed by Flow Pilots: http://www.flowpilots.com/

## Usage

You can install the latest version via npm:

$ npm install systemd

```sh
$ npm install systemd
```

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

require('systemd');
```javascript
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');
```

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
```ini
[Socket]
ListenStream=1337

[Install]
WantedBy=sockets.target
[Install]
WantedBy=sockets.target
```

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

# Adjust according to man 5 systemd.exec
```ini
# Adjust according to man 5 systemd.exec

[Service]
ExecStart=/path/to/bin/node /path/to/hello.js
StandardOutput=syslog
User=nobody
Group=nobody
[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][issue-11].

[issue-11]: https://github.com/rubenv/node-systemd/issues/11

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

systemctl --system daemon-reload
```sh
$ systemctl --system daemon-reload
```

Enable and start the socket:

systemctl enable node-hello.socket
systemctl start node-hello.socket
```sh
$ 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
```sh
$ 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
```sh
$ 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
```sh
$ 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
```sh
$ 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);
```javascript
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.
Expand All @@ -117,17 +144,24 @@

Install the dev dependencies:

npm install --dev
```sh
$ npm install --dev
```

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

npm -g install grunt-cli
```sh
$ npm -g install grunt-cli
```

Run it:

grunt
```sh
$ grunt
```


## License
## License

(The MIT License)

Expand All @@ -150,3 +184,4 @@
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.