# Timeout / early kill an external process

**URL:** <https://discourse.julialang.org/t/timeout-early-kill-an-external-process/53876>\
**Category:** New to Julia\
**Tags:** linux, task\
**Created:** [January 24, 2021, 8:46pm UTC](https://discourse.julialang.org/t/timeout-early-kill-an-external-process/53876 "2021-01-24T20:46:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![s-baumann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/s-baumann/32/5843_2.png) [@s-baumann](https://discourse.julialang.org/u/s-baumann)\
**Post date:** [January 24, 2021, 8:46pm UTC](https://discourse.julialang.org/t/timeout-early-kill-an-external-process/53876/1 "2021-01-24T20:46:03Z")

</div>

I want to do a terminal command (on linux) with a timeout. I know there is not a timeout built into the run command so I was trying to do something to make one. So basically I tried:

```julia
function run_with_timeout(command::String, timeout::Integer = 5)
    cmd = run(`$command`; wait = false) 
     for i in 1:timeout 
         if success(cmd) return :success end 
         sleep(1)
     end
     kill(cmd) 
     return :failed
end
command = "sleep 3"
run_with_timeout(command)

```

This doesn’t work because success(cmd) waits until the process finishes rather than immediately saying whether or not the process has finished yet.

Is there a function that immediately says whether or not a process is finished yet? Any other way to achieve this goal?

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [January 24, 2021, 9:08pm UTC](https://discourse.julialang.org/t/timeout-early-kill-an-external-process/53876/2 "2021-01-24T21:08:59Z")

</div>

Can you call `istaskdone` on the cmd?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [January 24, 2021, 9:09pm UTC](https://discourse.julialang.org/t/timeout-early-kill-an-external-process/53876/3 "2021-01-24T21:09:13Z")

</div>

it’s called `process_running` for `run()` (which creates process with some pid)

```julia
julia> function run_with_timeout(command, timeout::Integer = 5)
           cmd = run(command; wait=false)
            for i in 1:timeout
                if !process_running(cmd) return success(cmd) end
                sleep(1)
            end
            kill(cmd)
            return false
       end

julia> @time run_with_timeout(`sleep 7`)
  5.010722 seconds (48 allocations: 2.062 KiB)
false

julia> @time run_with_timeout(`sleep 2`)
  2.003042 seconds (37 allocations: 1.703 KiB)
true

```

* * *

btw I changed your function to return a boolean instead of symbol, and also to return `false` in case where `cmd` finished running with an error

---

<div class="post-metadata">

**Author:** ![remi-garcia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/remi-garcia/32/7125_2.png) [@remi-garcia](https://discourse.julialang.org/u/remi-garcia)\
**Post date:** [June 12, 2025, 10:27pm UTC](https://discourse.julialang.org/t/timeout-early-kill-an-external-process/53876/4 "2025-06-12T22:27:19Z")

</div>

This topic is old but I just want to add something to the solution here.

`kill(cmd)` is not enough to kill subprocesses. To kill “everything” ([https://unix.stackexchange.com/questions/706765](https://unix.stackexchange.com/questions/706765)) the following is necessary:

```julia
curr_pid = getpid(cmd)
killcmd = "pkill -P $(curr_pid)"
argv = Vector{String}(string.(split(killcmd)))
run(`$(argv)`)

```
