DataSift\Storyplayer\OsLib\Base_Unix::getPid PHP Method

getPid() public method

public getPid ( HostDetails $hostDetails, string $processName ) : integer
$hostDetails HostDetails
$processName string
return integer
    public function getPid($hostDetails, $processName)
    {
        // log some info to the user
        $log = usingLog()->startAction("get PID for process '{$processName}' running on host '{$hostDetails->hostId}'");
        // run the command to get the process id
        $command = "ps -ef | grep '{$processName}'";
        $result = $this->runCommand($hostDetails, $command);
        // check that we got something
        if ($result->didCommandFail() || empty($result->output)) {
            $log->endAction("could not get process list");
            return 0;
        }
        // reduce the output down to a single pid
        $pids = explode("\n", $result->output);
        $pids = FilterForMatchingRegex::against($pids, "/{$processName}/");
        $pids = FilterColumns::from($pids, "1", ' ');
        // check that we found exactly one process
        if (count($pids) != 1) {
            $log->endAction("found more than one process but expecting only one ... is this correct?");
            return 0;
        }
        // we can now reason that we have the correct pid
        $pid = $pids[0];
        // all done
        $log->endAction("{$pid}");
        return $pid;
    }