|
3 | 3 | Adds support for running node.js as a socket-activated service under systemd.
|
4 | 4 |
|
5 | 5 | More info on the how and why: http://savanne.be/articles/deploying-node-js-with-systemd/
|
6 |
| - |
| 6 | + |
7 | 7 | For more background on socket activation: http://0pointer.de/blog/projects/socket-activation.html
|
8 | 8 |
|
9 | 9 | Obviously, this will only work on Linux distributions with systemd (such as Fedora).
|
10 | 10 |
|
11 | 11 | Developed by Flow Pilots: http://www.flowpilots.com/
|
12 | 12 |
|
13 | 13 | ## Usage
|
14 |
| - |
| 14 | + |
15 | 15 | You can install the latest version via npm:
|
16 |
| - |
17 |
| - $ npm install systemd |
| 16 | + |
| 17 | +```sh |
| 18 | +$ npm install systemd |
| 19 | +``` |
18 | 20 |
|
19 | 21 | Require the systemd module and pass 'systemd' as a parameter to listen():
|
20 | 22 |
|
21 |
| - require('systemd'); |
| 23 | +```javascript |
| 24 | +require('systemd'); |
| 25 | + |
| 26 | +var http = require('http'); |
| 27 | +http.createServer(function (req, res) { |
| 28 | + res.writeHead(200, {'Content-Type': 'text/plain'}); |
| 29 | + res.end('Hello World\n'); |
| 30 | +}).listen('systemd'); |
| 31 | +``` |
22 | 32 |
|
23 |
| - var http = require('http'); |
24 |
| - http.createServer(function (req, res) { |
25 |
| - res.writeHead(200, {'Content-Type': 'text/plain'}); |
26 |
| - res.end('Hello World\n'); |
27 |
| - }).listen('systemd'); |
28 |
| - |
29 | 33 | Install a systemd socket file (e.g.: /etc/systemd/system/node-hello.socket):
|
30 | 34 |
|
31 |
| - [Socket] |
32 |
| - ListenStream=1337 |
| 35 | +```ini |
| 36 | +[Socket] |
| 37 | +ListenStream=1337 |
33 | 38 |
|
34 |
| - [Install] |
35 |
| - WantedBy=sockets.target |
| 39 | +[Install] |
| 40 | +WantedBy=sockets.target |
| 41 | +``` |
36 | 42 |
|
37 | 43 | Install a systemd service file (e.g.: /etc/systemd/system/node-hello.service):
|
38 | 44 |
|
39 |
| - # Adjust according to man 5 systemd.exec |
| 45 | +```ini |
| 46 | +# Adjust according to man 5 systemd.exec |
40 | 47 |
|
41 |
| - [Service] |
42 |
| - ExecStart=/path/to/bin/node /path/to/hello.js |
43 |
| - StandardOutput=syslog |
44 |
| - User=nobody |
45 |
| - Group=nobody |
| 48 | +[Service] |
| 49 | +ExecStart=/path/to/bin/node /path/to/hello.js |
| 50 | +StandardOutput=syslog |
| 51 | +User=nobody |
| 52 | +Group=nobody |
| 53 | +``` |
46 | 54 |
|
47 | 55 | Be sure to substitute the paths to node and your script!
|
48 | 56 |
|
| 57 | + * ⚠ __Run node directly__ or make sure your startup helper scripts |
| 58 | + can hand over the sockets. `npm start` [probably won't work][issue-11]. |
| 59 | + |
| 60 | + [issue-11]: https://github.com/rubenv/node-systemd/issues/11 |
| 61 | + |
49 | 62 | Reload the systemd daemon so that it picks up the new unit files:
|
50 | 63 |
|
51 |
| - systemctl --system daemon-reload |
| 64 | +```sh |
| 65 | +$ systemctl --system daemon-reload |
| 66 | +``` |
52 | 67 |
|
53 | 68 | Enable and start the socket:
|
54 | 69 |
|
55 |
| - systemctl enable node-hello.socket |
56 |
| - systemctl start node-hello.socket |
| 70 | +```sh |
| 71 | +$ systemctl enable node-hello.socket |
| 72 | +$ systemctl start node-hello.socket |
| 73 | +``` |
57 | 74 |
|
58 | 75 | Check the status of the socket:
|
59 | 76 |
|
60 |
| - # systemctl status node-hello.socket |
61 |
| - node-hello.socket |
62 |
| - Loaded: loaded (/etc/systemd/system/node-hello.socket) |
63 |
| - Active: active (listening) since Sat, 15 Oct 2011 20:27:47 +0200; 2s ago |
64 |
| - CGroup: name=systemd:/system/node-hello.socket |
| 77 | +```sh |
| 78 | +$ systemctl status node-hello.socket |
| 79 | +node-hello.socket |
| 80 | + Loaded: loaded (/etc/systemd/system/node-hello.socket) |
| 81 | + Active: active (listening) since Sat, 15 Oct 2011 20:27:47 +0200; 2s ago |
| 82 | + CGroup: name=systemd:/system/node-hello.socket |
| 83 | +``` |
65 | 84 |
|
66 | 85 | Great, it's running!
|
67 | 86 |
|
68 | 87 | Check the status of the service, not running yet:
|
69 | 88 |
|
70 |
| - # systemctl status node-hello.service |
71 |
| - node-hello.service |
72 |
| - Loaded: loaded (/etc/systemd/system/node-hello.service) |
73 |
| - Active: inactive (dead) |
74 |
| - CGroup: name=systemd:/system/node-hello.service |
| 89 | +```sh |
| 90 | +$ systemctl status node-hello.service |
| 91 | +node-hello.service |
| 92 | + Loaded: loaded (/etc/systemd/system/node-hello.service) |
| 93 | + Active: inactive (dead) |
| 94 | + CGroup: name=systemd:/system/node-hello.service |
| 95 | +``` |
75 | 96 |
|
76 | 97 | Do a request to your service:
|
77 | 98 |
|
78 |
| - # curl -i http://localhost:1337/ |
79 |
| - HTTP/1.1 200 OK |
80 |
| - Content-Type: text/plain |
81 |
| - Connection: keep-alive |
82 |
| - Transfer-Encoding: chunked |
83 |
| - |
84 |
| - Hello World |
| 99 | +```sh |
| 100 | +$ curl -i http://localhost:1337/ |
| 101 | +HTTP/1.1 200 OK |
| 102 | +Content-Type: text/plain |
| 103 | +Connection: keep-alive |
| 104 | +Transfer-Encoding: chunked |
| 105 | + |
| 106 | +Hello World |
| 107 | +``` |
85 | 108 |
|
86 | 109 | Check again, now it will be running:
|
87 | 110 |
|
88 |
| - # systemctl status node-hello.service |
89 |
| - node-hello.service |
90 |
| - Loaded: loaded (/etc/systemd/system/node-hello.service) |
91 |
| - Active: active (running) since Sat, 15 Oct 2011 20:32:10 +0200; 38s ago |
92 |
| - Main PID: 1159 (node) |
93 |
| - CGroup: name=systemd:/system/node-hello.service |
94 |
| - └ 1159 /path/to/bin/node /path/to/hello.js |
| 111 | +```sh |
| 112 | +$ systemctl status node-hello.service |
| 113 | +node-hello.service |
| 114 | + Loaded: loaded (/etc/systemd/system/node-hello.service) |
| 115 | + Active: active (running) since Sat, 15 Oct 2011 20:32:10 +0200; 38s ago |
| 116 | + Main PID: 1159 (node) |
| 117 | + CGroup: name=systemd:/system/node-hello.service |
| 118 | + └ 1159 /path/to/bin/node /path/to/hello.js |
| 119 | +``` |
95 | 120 |
|
96 | 121 | ## Only listen to systemd when running under systemd
|
97 | 122 |
|
98 | 123 | You can make the systemd usage conditional by checking for the systemd environment variable:
|
99 | 124 |
|
100 |
| - var http = require('http'); |
101 |
| - |
102 |
| - require('systemd'); |
103 |
| - |
104 |
| - var port = process.env.LISTEN_PID > 0 ? 'systemd' : 1337; |
105 |
| - http.createServer(function (req, res) { |
106 |
| - res.writeHead(200, {'Content-Type': 'text/plain'}); |
107 |
| - res.end('Hello World\n'); |
108 |
| - }).listen(port); |
| 125 | +```javascript |
| 126 | +var http = require('http'); |
| 127 | + |
| 128 | +require('systemd'); |
| 129 | + |
| 130 | +var port = process.env.LISTEN_PID > 0 ? 'systemd' : 1337; |
| 131 | +http.createServer(function (req, res) { |
| 132 | + res.writeHead(200, {'Content-Type': 'text/plain'}); |
| 133 | + res.end('Hello World\n'); |
| 134 | +}).listen(port); |
| 135 | +``` |
109 | 136 |
|
110 | 137 | This makes it possible to run the script stand-alone in development, yet use systemd when started through systemd.
|
111 |
| - |
| 138 | + |
112 | 139 | ## Contributing
|
113 | 140 |
|
114 | 141 | A jshint file is included to check code style.
|
|
117 | 144 |
|
118 | 145 | Install the dev dependencies:
|
119 | 146 |
|
120 |
| - npm install --dev |
| 147 | +```sh |
| 148 | +$ npm install --dev |
| 149 | +``` |
121 | 150 |
|
122 | 151 | Install the grunt cli if you haven't already done so:
|
123 | 152 |
|
124 |
| - npm -g install grunt-cli |
| 153 | +```sh |
| 154 | +$ npm -g install grunt-cli |
| 155 | +``` |
125 | 156 |
|
126 | 157 | Run it:
|
127 | 158 |
|
128 |
| - grunt |
| 159 | +```sh |
| 160 | +$ grunt |
| 161 | +``` |
| 162 | + |
129 | 163 |
|
130 |
| -## License |
| 164 | +## License |
131 | 165 |
|
132 | 166 | (The MIT License)
|
133 | 167 |
|
|
150 | 184 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
151 | 185 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
152 | 186 | THE SOFTWARE.
|
| 187 | + |
0 commit comments