Skip to content

Commit c9880e3

Browse files
committed
child_process: control argv0 for spawned processes
In some cases it useful to control the value of `argv[0]`, c.f. - https://github.com/andrewffff/child_process_with_argv0 - https://github.com/andrep/argv0 This patch adds explicit support for setting the value of `argv[0]` when spawning a process.
1 parent de074fe commit c9880e3

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

doc/api/child_process.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ added: v0.1.90
293293
* `options` {Object}
294294
* `cwd` {String} Current working directory of the child process
295295
* `env` {Object} Environment key-value pairs
296+
* `argv0` {String} Explicitly set the value of `argv[0]` sent to the child
297+
process. This will be set to `command` if not specified.
296298
* `stdio` {Array|String} Child's stdio configuration. (See
297299
[`options.stdio`][`stdio`])
298300
* `detached` {Boolean} Prepare child to run independently of its parent
@@ -395,6 +397,14 @@ child.on('error', (err) => {
395397
});
396398
```
397399

400+
*Note: Certain platforms (OS X, Linux) will use the value of `argv[0]` for the
401+
process title while others (Windows, SunOS) will use `command`.*
402+
403+
*Note: Node.js currently overwrites `argv[0]` with `process.execPath` on
404+
startup, so `process.argv[0]` in a Node.js child process will not match the
405+
`argv0` parameter passed to `spawn` from the parent, retrieve it with the
406+
`process.argv0` property instead.*
407+
398408
#### options.detached
399409
<!-- YAML
400410
added: v0.7.10

lib/child_process.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,11 @@ function normalizeSpawnArguments(file /*, args, options*/) {
347347
}
348348
}
349349

350-
args.unshift(file);
350+
if (typeof options.argv0 === 'string') {
351+
args.unshift(options.argv0);
352+
} else {
353+
args.unshift(file);
354+
}
351355

352356
var env = options.env || process.env;
353357
var envPairs = [];
Lines changed: 18 additions & 0 deletions
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');

0 commit comments

Comments
 (0)