class ProcessHelper {

    static int runWithTimeout(long timeoutInSeconds, Process process) throws InterruptedException, IOException {
        Preconditions.checkArgument(timeoutInSeconds > 0, "negative timeout: %s", timeoutInSeconds);
        int exitCode;
        long startTimeNano = System.nanoTime();
        while (true) {
            try {
                exitCode = process.exitValue();
                closeStream(process);
                return exitCode;
            } catch (IllegalThreadStateException ex) {
                long running = System.nanoTime() - startTimeNano;
                if (running > TimeUnit.SECONDS.toNanos(timeoutInSeconds)) {
                    break;
                }
                TimeUnit.NANOSECONDS.sleep(smartWait(running, startTimeNano, timeoutInSeconds));
            }
        }
        process.destroy();
        exitCode = 124;

        closeStream(process);
        return exitCode;
    }

    private  ProcessHelper();

}