# Read lines from command and kill the process

**URL:** <https://discourse.julialang.org/t/read-lines-from-command-and-kill-the-process/29600>\
**Category:** General Usage\
**Created:** [October 7, 2019, 3:15pm UTC](https://discourse.julialang.org/t/read-lines-from-command-and-kill-the-process/29600 "2019-10-07T15:15:58Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![sdewaele](https://avatars.discourse-cdn.com/v4/letter/s/7ab992/32.png) [@sdewaele](https://discourse.julialang.org/u/sdewaele)\
**Post date:** [October 7, 2019, 3:15pm UTC](https://discourse.julialang.org/t/read-lines-from-command-and-kill-the-process/29600/1 "2019-10-07T15:15:58Z")

</div>

I would like to do the following:

1. Start an external command
2. Read lines from the command
3. If the line contains a certain text, kill the external command
4. Return once all lines are read

Is there a simple way to achieve this? The best I have come up with is this:

```julia
function getoutput()
  write("tmp.txt","line 1\nline 2\nline 3")
  out = PipeBuffer()
  process = run(pipeline(`cat tmp.txt`,out),wait=false)
  lines = String[]
  lookfordata = true
  while lookfordata
    l = readline(out)
    isempty(l) || push!(lines,l)
    occursin("2",l) && (kill(process); break)
    sleep(0.001) # helper line, without which the while never returns
    lookfordata = !eof(out) || process_running(process)
  end
  lines
end
lines = getoutput()
display(lines)

```

Note in particular the “helper line” `sleep(0.001)`. Without this, or another “helper line”, the while loop never returns. Another helper line that does the trick is a print statement, e.g. `println("Hello!")`. This is puzzling to me…

I have tried using `eachline(`cat tmp.txt`)` in a for-loop:

```julia
for l in eachline(`cat tmp.txt`)
  push!(lines,l)
  occursin("2",l) && break
end

```

This is much more compact. However, now the `break` does not kill the process, so it does not meet my requirements.

I am using Julia 1.1. I am happy to hear any thoughts!
