Terminology (6)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Argument vector (argv)
- The list of strings a process receives when it starts. By convention the first names the program; the arguments follow.
- Environment
- A list of NAME=value strings a process receives when it starts. A child gets a copy of the environment it is given; later changes on either side stay in that process.
- Inherit
- Start a child with a copy of the parent’s current environment, which is what process-start APIs do when no environment is specified.
- Unset versus empty
- An unset variable has no entry; NAME= is an entry whose value is empty. Programs can tell them apart.
- Shell
- A program such as /bin/sh that parses a command line, splitting words and interpreting quotes and $, before starting a process with the resulting argument list.
- process.argv / process.env
- Node’s argument array (node path, script path, then arguments) and environment object.
What startup supplies
The starting process supplies an argument vector, an ordered list of strings, and an environment, a set of named string values. An argument can contain spaces or be empty. A shell parses command text to produce that list; a direct launch API accepts the elements themselves.
On Linux, an exec operation supplies those inputs to the kernel’s execve boundary. Exec replaces a process’s program image; the launch APIs used here arrange a separate child before that replacement. The child receives copies of the supplied arguments and environment; later changes stay in the process that makes them. Its runtime and initialization code then determine what happens before the application’s main work.
01What a process receives
Two lists of strings, copied at the start
A nightly report launcher starts one report process per job. The report titled “Q3 sales” arrives as “Q3”, and a region set for one job shows up in the next. What does a new process actually receive when it starts, and who decides it?
| Part | Form | Who decides it |
|---|---|---|
| Argument vector | Ordered strings, including spaces and empty values; no embedded NUL on Linux | The starting process, element by element |
| Environment | List of NAME=value strings | The starting process: usually its own, sometimes edited |
| Program image | The executable file, mapped into fresh memory | The path the starter names |
02The argument vector
Where your arguments start depends on the runtime
Start with the truncated title. Reduce the report child to an argument printer and give it show, two words and an empty string. This separates the launcher’s argument boundaries from the report formatter. The child finds show, counts the entries before it and prints the remaining list. Assume the child starts successfully.
import { spawnSync } from 'node:child_process';
function main(): void {
const index = process.argv.indexOf('show');
if (index >= 0) {
console.log(`entries before show: ${index}`);
console.log(`arguments: ${JSON.stringify(process.argv.slice(index))}`);
return;
}
spawnSync(process.execPath, [process.argv[1]!, 'show', 'two words', ''], {
stdio: 'inherit'
});
}
main();
npx tsc --noEmit --strict main.ts && node main.ts
03The environment block
A child starts with a copy of an environment
With the title boundary fixed, check the region that leaks into the next job. Could a report child have changed its launcher’s environment? In this probe, the launcher sets MODE, starts a child and waits. The child prints and changes MODE; the launcher then prints its own value.
import { spawnSync } from 'node:child_process';
function main(): void {
if (process.argv[2] === 'child') {
console.log(`child starts with MODE=${process.env.MODE}`);
process.env.MODE = 'child';
console.log(`child changed MODE=${process.env.MODE}`);
return;
}
process.env.MODE = 'launcher';
spawnSync(process.execPath, [process.argv[1]!, 'child'], { stdio: 'inherit' });
console.log(`launcher still has MODE=${process.env.MODE}`);
}
main();
04Before main
What runs before your main
The launcher may also change normal environment variables between jobs, so a reusable launch template needs a clear snapshot lifetime. The report loads settings too. If that loading happens in a global initializer, would setting configuration at the beginning of main be early enough? The kernel and loader prepare the new program image and initial stack before its runtime reaches the application. The runtime then follows its language’s startup rules. This reduced probe prints when loading happens; it does not itself read configuration. Node loads a script whose main function is an ordinary function call.
const settings = loadSettings();
function loadSettings(): string {
console.log('loading settings');
return 'ready';
}
function main(): void {
console.log(`main starts with settings=${settings}`);
}
main();
05Practice
Start each report with what it needs
The launcher’s remaining requirement is to keep its dispatch token for its own work while withholding it from report children. Normal variables may change between jobs; each report needs the current values, not a template saved for an earlier job. The lab continues the title and region repairs, then tests that boundary across multiple jobs and similarly named variables. Its report child prints what it actually received; these are real child processes, not the process-table model from the preceding lesson. Stdin uses job <title>|<region> and env NAME=value lines; the first separator divides each pair, and a missing or empty region means unset in the child.
The nightly report launcher
One launcher, real child processes. Pass each job’s title as one argument, give each report its own region, then keep a secret out of every report without losing it in the launcher.
Check this idea later
In the frontend Build-time public settings are earlier snapshots. Optional
A report-launcher dashboard shows a public region label. Changing the container’s startup environment can configure its server process correctly while leaving that label unchanged: the browser code may already contain a value captured during the frontend build. Check which process read the variable and when.
'use client';
export default function RegionLabel() {
return <p>{process.env.NEXT_PUBLIC_REGION}</p>;
}
Next.js inlines a direct NEXT_PUBLIC_REGION reference into browser code at build time. Promoting the same built artifact to a container with another startup value does not replace that embedded label. Values required at runtime need a server-to-client data path designed for that purpose. Public variables are visible to browser users.
Sources: Next.js: environment variables,SvelteKit: explicit environment variables.
Sign in to keep your read marks on every device. Sign in
Was this helpful?