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

Commit a98c03c

Browse files
authored
Merge pull request #14 from mk-pmb/readme-hint-npm
[doc][readme] hint: run node directly
2 parents 765a658 + f072311 commit a98c03c

File tree

1 file changed

+97
-62
lines changed

1 file changed

+97
-62
lines changed

README.md

Lines changed: 97 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,112 +3,139 @@
33
Adds support for running node.js as a socket-activated service under systemd.
44

55
More info on the how and why: http://savanne.be/articles/deploying-node-js-with-systemd/
6-
6+
77
For more background on socket activation: http://0pointer.de/blog/projects/socket-activation.html
88

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

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

1313
## Usage
14-
14+
1515
You can install the latest version via npm:
16-
17-
$ npm install systemd
16+
17+
```sh
18+
$ npm install systemd
19+
```
1820

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

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+
```
2232

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-
2933
Install a systemd socket file (e.g.: /etc/systemd/system/node-hello.socket):
3034

31-
[Socket]
32-
ListenStream=1337
35+
```ini
36+
[Socket]
37+
ListenStream=1337
3338

34-
[Install]
35-
WantedBy=sockets.target
39+
[Install]
40+
WantedBy=sockets.target
41+
```
3642

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

39-
# Adjust according to man 5 systemd.exec
45+
```ini
46+
# Adjust according to man 5 systemd.exec
4047

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+
```
4654

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

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+
4962
Reload the systemd daemon so that it picks up the new unit files:
5063

51-
systemctl --system daemon-reload
64+
```sh
65+
$ systemctl --system daemon-reload
66+
```
5267

5368
Enable and start the socket:
5469

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+
```
5774

5875
Check the status of the socket:
5976

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+
```
6584

6685
Great, it's running!
6786

6887
Check the status of the service, not running yet:
6988

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+
```
7596

7697
Do a request to your service:
7798

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+
```
85108

86109
Check again, now it will be running:
87110

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+
```
95120

96121
## Only listen to systemd when running under systemd
97122

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

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+
```
109136

110137
This makes it possible to run the script stand-alone in development, yet use systemd when started through systemd.
111-
138+
112139
## Contributing
113140

114141
A jshint file is included to check code style.
@@ -117,17 +144,24 @@
117144

118145
Install the dev dependencies:
119146

120-
npm install --dev
147+
```sh
148+
$ npm install --dev
149+
```
121150

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

124-
npm -g install grunt-cli
153+
```sh
154+
$ npm -g install grunt-cli
155+
```
125156

126157
Run it:
127158

128-
grunt
159+
```sh
160+
$ grunt
161+
```
162+
129163

130-
## License
164+
## License
131165

132166
(The MIT License)
133167

@@ -150,3 +184,4 @@
150184
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
151185
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
152186
THE SOFTWARE.
187+

0 commit comments

Comments
 (0)