Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
Add timeout to wmic
Browse files Browse the repository at this point in the history
  • Loading branch information
mircearoata committed Mar 28, 2022
1 parent cde3bea commit 7767847
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import StreamZip from 'node-stream-zip';
// @ts-ignore
import * as win_ca from 'win-ca/api';
import {
debug,
debug, warn,
} from './logging';
import { NetworkError } from './errors';
import {
Expand Down Expand Up @@ -243,19 +243,30 @@ export function mergeArrays<T>(...arrays: Array<Array<T>>): Array<T> {
return uniqueArray;
}

let defaultRunningMethodTimeout = false;

export async function isRunning(command: string, strict = false): Promise<boolean> {
try {
// manual is now main to handle ghost instances
if (defaultRunningMethodTimeout) {
throw new Error('Previous runs of isRunning timed out, using fallback method.');
}
let cmd = '';
switch (process.platform) {
case 'win32': cmd = `wmic process where (caption="${command}" and handlecount!="0") get commandline`; break;
case 'darwin': cmd = `ps -ax | grep ${command}`; break;
case 'linux': cmd = `ps -A | grep ${command}`; break;
default: break;
}
const runningInstances = execSync(cmd, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] });
const runningInstances = execSync(cmd, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'], timeout: 500 });
return runningInstances.toLowerCase().indexOf(command.toLowerCase()) > -1;
} catch (e) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if ((e as any).code === 'ETIMEDOUT') {
defaultRunningMethodTimeout = true;
warn('isRunning timed out, using fallback method.');
}

// fallback to psList
let runningInstances = [];
if (process.platform === 'win32' || strict) {
Expand Down

0 comments on commit 7767847

Please sign in to comment.