# Measuring stats (cpu time etc.) for external program

**URL:** <https://discourse.julialang.org/t/measuring-stats-cpu-time-etc-for-external-program/130017>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 19, 2025, 8:03am UTC](https://discourse.julialang.org/t/measuring-stats-cpu-time-etc-for-external-program/130017 "2025-06-19T08:03:15Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![bubaya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bubaya/32/217387_2.png) [@bubaya](https://discourse.julialang.org/u/bubaya)\
**Post date:** [June 19, 2025, 8:03am UTC](https://discourse.julialang.org/t/measuring-stats-cpu-time-etc-for-external-program/130017/1 "2025-06-19T08:03:15Z")

</div>

I have a an external program `prog`, and I am writing a julia program that prepares various data sets and measures the run time of `prog` for each of the data sets. My question is about how to run `prog`. Of course, I am aware of `run`. However, I also want to satisfy the following demands:

- `prog` should be aborted if a run exceeds N seconds
- ideally, all running instances of `prog` should be aborted if I hit ^C in the REPL,
- ideally, I measure not elapsed time, but cpu time of prog.  
Therefore, I came up with this snippet. I refrained from using `@elapsed`, because I want to be closer to the actual cpu time of the process:

```julia
function test_runtime(timeout=Inf) 
    time_path = tempname()
    process = run(pipeline(`/usr/bin/time -f%U -o$time_path sleep 10000`; stdout=devnull), wait=false)
    future = @async begin
        wait(process)
        return open(io->tryparse(Float64, readline(io)), time_path)
    end
    try
        if timedwait(()->istaskdone(future), timeout; pollint=1) == :timed_out
            println("timeout; send sigint to process.")
            kill(process, Base.SIGINT)
        end
        return fetch(future)
    catch e 
        if isa(e, InterruptException)
            println("Interrupted; send interrupt to process.")
            kill(process, Base.SIGINT)
        else
            rethrow(e)
        end
    end
end

```

my concerns are:

- the detour via `time` with the temporary file for the output is rather clumsy. Ideally, I would have a builtin access to the stats of the subprocess, but this seems impossible.
- The subprocess (time and its child) are not properly killed. This seems to be no julia problem, but a problem that `time` ignores sigint. Sending sigterm would be an option, but this won’t kill the actual child of `time`.
- if I use `@elapsed` instead of `time`, killing the subprocess works, but I’d prefer having access to the cpu time instead of wall time.
