@appium/support
Version:
Support libs used across Appium packages
64 lines • 2.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getProcessIds = getProcessIds;
exports.killProcess = killProcess;
const teen_process_1 = require("teen_process");
/*
* Exit Status for pgrep and pkill (`man pkill`)
* 0. One or more processes matched the criteria.
* 1. No processes matched.
* 2. Syntax error in the command line.
* 3. Fatal error: out of memory etc.
*/
/**
* Get PIDs of processes whose executable name matches the given name (exact match via pgrep -x).
*
* @param appName - Executable name to match (e.g. 'tail', 'node').
* @returns Promise resolving to an array of process IDs. Empty if no processes matched.
* @throws {Error} If pgrep fails for any reason other than "no processes matched" (exit 1).
* @deprecated Use a process-management API or package that fits your platform instead.
*/
async function getProcessIds(appName) {
let pids;
try {
const { stdout } = await (0, teen_process_1.exec)('pgrep', ['-x', appName]);
pids = stdout
.trim()
.split('\n')
.map((pid) => parseInt(pid, 10));
}
catch (err) {
const code = err.code;
if (code !== 1) {
throw new Error(`Error getting process ids for app '${appName}': ${err.message}`);
}
pids = [];
}
return pids;
}
/**
* Kill all processes whose executable name matches the given name (via pkill -x).
*
* @param appName - Executable name to match (e.g. 'tail', 'node').
* @param force - If true, use SIGKILL (-9); otherwise use default pkill signal.
* @returns Promise that resolves when done, or when no matching processes were running.
* @throws {Error} If pkill fails for any reason other than "no processes matched" (exit 1).
* @deprecated Use a process-management API or package that fits your platform instead.
*/
async function killProcess(appName, force = false) {
const pids = await getProcessIds(appName);
if (pids.length === 0) {
return;
}
try {
const args = force ? ['-9', '-x', appName] : ['-x', appName];
await (0, teen_process_1.exec)('pkill', args);
}
catch (err) {
const code = err.code;
if (code !== 1) {
throw new Error(`Error killing app '${appName}' with pkill: ${err.message}`);
}
}
}
//# sourceMappingURL=process.js.map