feat: improve getProcessInfo robustness with try-catch and empty output fallback

This commit is contained in:
xuewenjie
2025-12-22 11:38:38 +08:00
parent 4e7929850c
commit f33f43e2f7

View File

@@ -20,11 +20,26 @@ async function getProcessInfo(pid: number): Promise<{
command: string; command: string;
}> { }> {
// Only used for Unix systems (macOS and Linux) // Only used for Unix systems (macOS and Linux)
const { stdout } = await execAsync(`ps -p ${pid} -o ppid=,comm=`); try {
const [ppidStr, ...commandParts] = stdout.trim().split(/\s+/); const command = `ps -o ppid=,command= -p ${pid}`;
const parentPid = parseInt(ppidStr, 10); const { stdout } = await execAsync(command);
const command = commandParts.join(' '); const trimmedStdout = stdout.trim();
return { parentPid, name: path.basename(command), command }; if (!trimmedStdout) {
return { parentPid: 0, name: '', command: '' };
}
const parts = trimmedStdout.split(/\s+/);
const ppidString = parts[0];
const parentPid = parseInt(ppidString, 10);
const fullCommand = trimmedStdout.substring(ppidString.length).trim();
const processName = path.basename(fullCommand.split(' ')[0]);
return {
parentPid: isNaN(parentPid) ? 1 : parentPid,
name: processName,
command: fullCommand,
};
} catch (_e) {
return { parentPid: 0, name: '', command: '' };
}
} }
/** /**
* Finds the IDE process info on Unix-like systems. * Finds the IDE process info on Unix-like systems.