Wrong pid returned in node executable

Working on a cli tool for my latest project. I'm writing a stop method and trying to kill the node process based on its pid.

This is the code I have but it looks like I'm only getting the pid for the grep function

#!/usr/bin/env node

(function () {
var args = process.argv.slice(2),
    exec = require('child_process').exec;

args.forEach(function (arg) {
    switch (arg) {
        case 'start':
            require('./library/Ubui');
            return;
        break;
        case 'stop':
            //I am aware this will kill the grep instance too
            //but I don't care. Why should we?
            exec('ps aux | grep ubui.js | cut -c11-15', function (a, b) {
                var ubuifs = b.split("\n");
                console.log(ubuifs);
                ubuifs.forEach(function (pid) {
                    if (pid !== '') {
                        console.log('\x1b[33mAttempting to kill process pid: ' + pid + '\x1b[0m');
                        exec('kill ' + pid, function (a, b) {
                            console.log(a, b);
                        });
                    }
                });
            });
        break;
    }
});
})();

If anyone has any idea you could answer here or do a pull request on the github here and explain why it isn't working? It would be a really handy cli tool if I could get this working!

Cheers

This is a hack, but it works.

On mac and linux, I have to add +1 or +2 to the exec'd PID depending on the app, cannot figure out why it's 1 or 2, doesn't seem to depend on node version or os. For a given app the offset is always the same, that I can say for sure.

What's interesting is that with child.spawn there's no adjustment required.

This solves your problem, but doesn't address the root cause. If anyone figures out what the offset is about in child.exec that'd be great.