# In a pipeline, interrupt only one command

**URL:** <https://discourse.julialang.org/t/in-a-pipeline-interrupt-only-one-command/76301>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 12, 2022, 4:12pm UTC](https://discourse.julialang.org/t/in-a-pipeline-interrupt-only-one-command/76301 "2022-02-12T16:12:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)\
**Post date:** [February 12, 2022, 4:12pm UTC](https://discourse.julialang.org/t/in-a-pipeline-interrupt-only-one-command/76301/1 "2022-02-12T16:12:37Z")

</div>

I need to pipe two programs, the first one can be interrupted only by sending a kill signal; how to kill it without killing the whole pipeline? For example how to interrupt `yes` in a moral equivalent of `pipeline(`yes`, `wc -l`)` using Ctrl-C, and get the line count? I tried doing `yes = open(`yes`)`, and running `run(pipeline(yes, `wc -l`))` asynchronously, but didn’t manage to catch `InterruptException` in a way that gives me back the wanted result.

EDIT: just running `run(pipeline(`yes`, `wc -l`))` doesn’t give the result after Ctrl-C, I assume because the second command `wc -l` is also killed by Ctrl-C before it has a chance to print the result.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [February 12, 2022, 8:09pm UTC](https://discourse.julialang.org/t/in-a-pipeline-interrupt-only-one-command/76301/2 "2022-02-12T20:09:01Z")

</div>

```julia
io = IOBuffer()
try
    p = run(pipeline(ignorestatus(`yes`); stdout=io))
catch e
    if e isa InterruptException
        kill(p, Base.SIGKILL)
    else
        rethrow()
    end
end
seekstart(io)
run(pipeline(`wc -l`; stdin=io))

```

Inspired by [https://github.com/JuliaLang/Pkg.jl/blob/b4da4946735fe4c7c6bb3e2bc16af95b4e76e487/src/Operations.jl#L1728-L1745](https://github.com/JuliaLang/Pkg.jl/blob/b4da4946735fe4c7c6bb3e2bc16af95b4e76e487/src/Operations.jl#L1728-L1745) (from this recent pull request which made Ctrl+C-ing tests much friendlier: [https://github.com/JuliaLang/Pkg.jl/pull/2933](https://github.com/JuliaLang/Pkg.jl/pull/2933))

---

<div class="post-metadata">

**Author:** ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)\
**Post date:** [February 12, 2022, 9:09pm UTC](https://discourse.julialang.org/t/in-a-pipeline-interrupt-only-one-command/76301/3 "2022-02-12T21:09:33Z")

</div>

Thanks Mosè! Yes that’s a decent solution, the main problem being that everything from `yes` has to fit in memory (and minor inconvenience being that `wc` can run only after `yes` stops). But i will use that (writing ro a file instead) if no other solution is found.
