Skip to content

Instantly share code, notes, and snippets.

@squarefeet
Created June 20, 2012 14:38
Show Gist options
  • Save squarefeet/2960203 to your computer and use it in GitHub Desktop.
Save squarefeet/2960203 to your computer and use it in GitHub Desktop.
Node PID bug
var exec = require('child_process').exec,
child = exec('node [PATH-TO-YOUR-KEEP-ALIVE-NODE-FILE]');
console.log('child pid: ' + child.pid);
exec('ps aux | grep node', function(err, stdout) {
console.log(stdout);
});
@sandfox
Copy link

sandfox commented Jun 20, 2012

Jamess-MacBook-Air:nodeBug the_rebel_child_$ node -v
v0.6.18
Jamess-MacBook-Air:nodeBug the_rebel_child_$ node test.js 
child pid: 68930
the_rebel_child_ 68928  33.4  0.3  3069196  13424 s001  S+    3:48pm   0:00.13 node test.js
the_rebel_child_ 68930   8.0  0.0  2437996   1448 s001  R+    3:48pm   0:00.05 node child.js
the_rebel_child_ 68931   0.7  0.0  2435488    808 s001  S+    3:48pm   0:00.01 /bin/sh -c ps aux | grep node
the_rebel_child_ 68933   0.0  0.0  2425480    260 s001  R+    3:48pm   0:00.00 grep node

@squarefeet
Copy link
Author

Lukes-MacBook-Pro:pid luke$ node -v
v0.6.6
Lukes-MacBook-Pro:pid luke$ node test.js
child pid: 71578
luke     71577  20.6  0.3  3045180  10764 s002  S+    3:53pm   0:00.07 node test.js
luke     71578   2.5  0.2  3044564   9304 s002  R+    3:53pm   0:00.08 node child.js
luke     71581   0.0  0.0  2435116    528 s002  S+    3:53pm   0:00.00 grep node
luke     71579   0.0  0.0  2435544    744 s002  S+    3:53pm   0:00.02 /bin/sh -c ps aux | grep node
luke     71433   0.0  0.3  3047312  11136 s000  S+    3:42pm   0:00.42 node index.js

Something is going awry in my other Node files, then... this test is telling me it's all okay. Fucknuts.

@sandfox
Copy link

sandfox commented Jun 20, 2012

my child.js is this:

var http = require('http');
var server = http.createServer();
server.listen();

@squarefeet
Copy link
Author

Mine's very similar:

require('http').createServer().listen(23984);

I'm going to dig deeper and see if it's anything to do with the REPL I'm using.

@squarefeet
Copy link
Author

Think this is the cause:

luke     71743   0.0  0.0  2435116    524 s001  R+    4:05pm   0:00.00 grep node
luke     71733   0.0  0.4  3048568  17616 s000  S+    4:05pm   0:00.31 node test.js
luke     71732   0.0  0.0  2435544    768 s000  S+    4:05pm   0:00.00 /bin/sh -c cd node_modules/grppl-simserver && node test.js
luke     71730   0.0  0.3  3047240  13280 s000  S+    4:05pm   0:00.15 node index.js

See that /bin/sh -c ... line? That's the process that child.pid is reporting back. SIGTERM-ing that process does fuck all. To kill the child process, you need to kill node test.js, which is another process spawned straight after the exec command.

So.. exec creates one process, and 'cos I'm spinning up a new node instance within that exec child proc, it'll have a diff. pid than the exec proc.

Bit of a derp moment :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment