MCPcopy
hub / github.com/apify/crawlee / psTree

Function psTree

packages/utils/src/internals/systemInfoV2/ps-tree.ts:27–138  ·  view source on GitHub ↗
(pid: number | string, includeRoot = false)

Source from the content-addressed store, hash-verified

25 * @internal
26 */
27export async function psTree(pid: number | string, includeRoot = false): Promise<ProcessInfo[]> {
28 return new Promise((resolve, reject) => {
29 if (typeof pid === 'number') {
30 pid = pid.toString();
31 }
32
33 let processLister: ChildProcess;
34 if (process.platform === 'win32') {
35 processLister = spawn('powershell', [
36 '-NoProfile',
37 '-Command',
38 'Get-CimInstance Win32_Process | Format-Table ProcessId,ParentProcessId,WorkingSetSize,Name',
39 ]);
40 } else {
41 processLister = spawn('ps', ['-A', '-o', 'ppid,pid,stat,rss,comm']);
42 }
43
44 processLister.on('error', reject);
45
46 if (!processLister.stdout) {
47 reject(new Error('Child process stdout is null'));
48 return;
49 }
50
51 // Create a readline interface to process stdout line-by-line.
52 const rl = readline.createInterface({
53 input: processLister.stdout,
54 });
55
56 const rows: ProcessInfo[] = [];
57 let headers: string[] | null = null;
58
59 rl.on('line', (line: string) => {
60 const trimmed = line.trim();
61 if (trimmed === '') {
62 return; // Skip empty lines.
63 }
64
65 // When headers have been set, skip a dashed separator line.
66 const fields = trimmed.split(/\s+/);
67 if (headers !== null && fields.every((field) => /^-+$/.test(field))) {
68 return;
69 }
70
71 // The first nonempty line is assumed to be the header row.
72 if (!headers) {
73 headers = fields.map(normalizeHeader);
74 return;
75 }
76
77 // Copy the fields into an array to process.
78 const columns = fields.slice();
79
80 // Build the process row object.
81 const row: Partial<ProcessInfo> = {};
82 const hdrs = headers.slice();
83 // For all headers except the last one, assign one column per header.
84 // The last header gets all remaining columns joined (in case the command name contains spaces).

Callers 3

getMemoryInfoV2Function · 0.90
waitForConditionFunction · 0.90
psTree.test.tsFile · 0.90

Calls 5

toStringMethod · 0.80
mapMethod · 0.80
pushMethod · 0.80
forEachMethod · 0.80
entriesMethod · 0.65

Tested by 1

waitForConditionFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…