diff --git a/.gitignore b/.gitignore index ecb9a1f..da2cf06 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ *.iws **/node_modules node_modules -.idea \ No newline at end of file +.idea +.vscode/ diff --git a/lib/index.js b/lib/index.js index 893c57b..3ffec08 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,6 +1,7 @@ var ChildProcess = require('child_process'); var IS_WIN = process.platform === 'win32'; var TableParser = require('table-parser'); +var fs = require('fs'); /** * End of line. * Basically, the EOL should be: @@ -273,6 +274,21 @@ function formatOutput(data) { var ppid = ( d.PPID && d.PPID[0] ) || ( d.ParentProcessId && d.ParentProcessId[0] ) || undefined; if (pid && cmd) { + // console.log('Print `%j`', cmd) + if (!IS_WIN && cmd.length > 1) { + // console.log('Print yes `%j`', cmd) + try { + cmd = fs.readFileSync('/proc/' + pid + '/cmdline', 'utf8').split('\0'); + } + catch (err) { + if (err.code !== 'ENOENT') { + throw err; + } + } + // console.log('data: `%j`', data); + // cmd = fs.readFileSync('/proc/' + pid + '/cmdline').split('\0'); + } + var command = cmd[0]; var args = ''; diff --git a/test/test.js b/test/test.js index a682004..bbd8e2c 100644 --- a/test/test.js +++ b/test/test.js @@ -5,6 +5,7 @@ var Path = require('path'); var Sinon = require('sinon'); var serverPath = Path.resolve(__dirname, './node_process_for_test.js'); +var serverPathSpace = Path.resolve(__dirname, './with space/sleep with space.sh'); var UpperCaseArg = '--UPPER_CASE'; var child = null; var pid = null; @@ -197,3 +198,23 @@ describe('test', function () { }); }); }); + +// don't run on Windows +(process.platform === 'win32' ? describe.skip : describe)('test command with space', function () { + before(function () { + child = CP.spawn(serverPathSpace); + pid = child.pid; + }); + + afterEach(killProcess); + + it('by command with space in path', function (done) { + PS.lookup({pid: pid}, function (err, list) { + assert.equal(list.length, 1); + assert.equal(list[0].pid, pid); + assert.equal(list[0].command, '/bin/bash'); + assert.equal(list[0].arguments[0], serverPathSpace); + done(); + }); + }); +}); \ No newline at end of file diff --git a/test/with space/sleep with space b/test/with space/sleep with space new file mode 100644 index 0000000..1b3f24d --- /dev/null +++ b/test/with space/sleep with space @@ -0,0 +1,2 @@ +#!/bin/bash +/bin/sleep 5 diff --git a/test/with space/sleep with space.sh b/test/with space/sleep with space.sh new file mode 100755 index 0000000..1b3f24d --- /dev/null +++ b/test/with space/sleep with space.sh @@ -0,0 +1,2 @@ +#!/bin/bash +/bin/sleep 5