isRunning static method

Future<bool> isRunning(
  1. int pid
)

True when the process pid is running: false once exited (a zombie, not reaped yet by its parent, is not running).

Linux reads /proc/<pid>/stat, macOS runs ps, Windows tasklist.

Implementation

static Future<bool> isRunning(int pid) async {
  if (io.Platform.isLinux) {
    var stat = _readLinuxProcStat(pid);
    return stat != null && !_isExitedState(stat.state);
  }
  try {
    if (io.Platform.isWindows) {
      var result = await io.Process.run('tasklist', [
        '/FI',
        'PID eq $pid',
        '/FO',
        'CSV',
        '/NH',
      ]);
      return result.stdout.toString().contains('"$pid"');
    }
    var result = await io.Process.run('ps', ['-o', 'stat=', '-p', '$pid']);
    var state = result.stdout.toString().trim();
    return result.exitCode == 0 && state.isNotEmpty && !_isExitedState(state);
  } catch (_) {
    return false;
  }
}