child_process: control argv0 for spawned processes · nodejs/node@99f45b2

3 files changed

lines changed

Original file line numberDiff line numberDiff line change

@@ -295,6 +295,8 @@ added: v0.1.90

295295

* `options` {Object}

296296

* `cwd` {String} Current working directory of the child process

297297

* `env` {Object} Environment key-value pairs

298+

* `argv0` {String} Explicitly set the value of `argv[0]` sent to the child

299+

process. This will be set to `command` if not specified.

298300

* `stdio` {Array|String} Child's stdio configuration. (See

299301

[`options.stdio`][`stdio`])

300302

* `detached` {Boolean} Prepare child to run independently of its parent

@@ -397,6 +399,14 @@ child.on('error', (err) => {

397399

});

398400

```

399401
402+

*Note: Certain platforms (OS X, Linux) will use the value of `argv[0]` for the

403+

process title while others (Windows, SunOS) will use `command`.*

404+
405+

*Note: Node.js currently overwrites `argv[0]` with `process.execPath` on

406+

startup, so `process.argv[0]` in a Node.js child process will not match the

407+

`argv0` parameter passed to `spawn` from the parent, retrieve it with the

408+

`process.argv0` property instead.*

409+
400410

#### options.detached

401411

<!-- YAML

402412

added: v0.7.10

Original file line numberDiff line numberDiff line change

@@ -351,7 +351,11 @@ function normalizeSpawnArguments(file /*, args, options*/) {

351351

}

352352

}

353353
354-

args.unshift(file);

354+

if (typeof options.argv0 === 'string') {

355+

args.unshift(options.argv0);

356+

} else {

357+

args.unshift(file);

358+

}

355359
356360

var env = options.env || process.env;

357361

var envPairs = [];

Original file line numberDiff line numberDiff line change

@@ -0,0 +1,18 @@

1+

'use strict';

2+

require('../common');

3+

const assert = require('assert');

4+

const cp = require('child_process');

5+
6+

// This test spawns itself with an argument to indicate when it is a child to

7+

// easily and portably print the value of argv[0]

8+

if (process.argv[2] === 'child') {

9+

console.log(process.argv0);

10+

return;

11+

}

12+
13+

const noArgv0 = cp.spawnSync(process.execPath, [__filename, 'child']);

14+

assert.strictEqual(noArgv0.stdout.toString().trim(), process.execPath);

15+
16+

const withArgv0 = cp.spawnSync(process.execPath, [__filename, 'child'],

17+

{argv0: 'withArgv0'});

18+

assert.strictEqual(withArgv0.stdout.toString().trim(), 'withArgv0');