@@ -1547,6 +1547,42 @@ exited, `code` is the final exit code of the process, otherwise `null`. If the
15471547process terminated due to receipt of a signal, ` signal` is the string name of
15481548the signal, otherwise ` null ` . One of the two will always be non-` null ` .
15491549
1550+ When ` code` is ` null ` due to signal termination, you can use
1551+ [` util .convertProcessSignalToExitCode ()` ][] to convert the signal to a POSIX
1552+ exit code.
1553+
1554+ ` ` ` cjs
1555+ const { spawn } = require (' node:child_process' );
1556+ const { convertProcessSignalToExitCode } = require (' node:util' );
1557+
1558+ const ls = spawn (' ls' , [' -lh' , ' /usr' ]);
1559+
1560+ ls .kill ();
1561+
1562+ ls .on (' exit' , (code , signal ) => {
1563+ const exitCode = convertProcessSignalToExitCode (signal);
1564+ console .log (` signal ${ signal} , POSIX exit code: ${ exitCode} ` );
1565+ });
1566+
1567+ // signal SIGTERM, POSIX exit code: 143
1568+ ` ` `
1569+
1570+ ` ` ` mjs
1571+ import { spawn } from ' node:child_process' ;
1572+ import { once } from ' node:events' ;
1573+ import { convertProcessSignalToExitCode } from ' node:util' ;
1574+
1575+ const ls = spawn (' ls' , [' -lh' , ' /usr' ]);
1576+
1577+ ls .kill ();
1578+
1579+ const [code , signal ] = await once (ls, ' exit' );
1580+ const exitCode = convertProcessSignalToExitCode (signal);
1581+ console .log (` signal ${ signal} , POSIX exit code: ${ exitCode} ` );
1582+
1583+ // signal SIGTERM, POSIX exit code: 143
1584+ ` ` `
1585+
15501586When the ` ' exit' ` event is triggered, child process stdio streams might still be
15511587open.
15521588
@@ -1671,6 +1707,16 @@ within the child process to close the IPC channel as well.
16711707The ` subprocess .exitCode ` property indicates the exit code of the child process.
16721708If the child process is still running, the field will be ` null ` .
16731709
1710+ When the child process is terminated by a signal, ` subprocess .exitCode ` will be
1711+ ` null ` . To get the corresponding POSIX exit code, use
1712+ [` util .convertProcessSignalToExitCode (subprocess .signalCode )` ][` util .convertProcessSignalToExitCode ()` ].
1713+
1714+ **Note:** The ` child_process` module does not automatically set ` exitCode` when
1715+ a process is terminated by a signal to avoid breaking changes in existing code
1716+ that may depend on ` exitCode` being ` null ` in such cases. The
1717+ [` util .convertProcessSignalToExitCode ()` ][] utility function is provided to
1718+ allow applications to opt-in to this conversion when needed.
1719+
16741720### ` subprocess .kill ([signal])`
16751721
16761722<!-- YAML
@@ -2393,6 +2439,7 @@ or [`child_process.fork()`][].
23932439[`subprocess.stdin`]: #subprocessstdin
23942440[`subprocess.stdio`]: #subprocessstdio
23952441[`subprocess.stdout`]: #subprocessstdout
2442+ [`util.convertProcessSignalToExitCode()`]: util.md#utilconvertprocesssignaltoexitcodesignalcode
23962443[`util.promisify()`]: util.md#utilpromisifyoriginal
23972444[synchronous counterparts]: #synchronous-process-creation
23982445[v8.serdes]: v8.md#serialization-api
0 commit comments