# Reading output from external program in stdout

**URL:** https://discourse.julialang.org/t/reading-output-from-external-program-in-stdout/84289
**Category:** General Usage
**Tags:** question
**Created:** [July 15, 2022, 8:01pm UTC](https://discourse.julialang.org/t/reading-output-from-external-program-in-stdout/84289 "2022-07-15T20:01:58Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Elmo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmo/32/17979_2.png) [@Elmo](https://discourse.julialang.org/u/Elmo)
#### Post date: [July 15, 2022, 8:01pm UTC](https://discourse.julialang.org/t/reading-output-from-external-program-in-stdout/84289/1 "2022-07-15T20:01:58Z")

</div>

I am trying to use Julia to read messages sent to me through xmpp using [go-sendxmpp](https://salsa.debian.org/mdosch/go-sendxmpp). By following the readme of this package, I can successfully use the terminal to listen to all messages sent to me through:

```julia
PS C:\Users\user\xmpp> .\go-sendxmpp.exe --file="./config" --listen
2022-07-15T21:51:09+02:00 user@server: hello world!

```

So every time a message is sent to me, it writes the message on the stdout, as described in the documentation of the app.

I would like to write a Julia script that:

1. Catches each line (message) as a string,
2. Does something to the string e.g. split it into time and payload,
3. Waits for the next string and repeats the loop.

The overall goal is to send commands to via the xmpp to my computer that Julia executes (I cannot use Genie or Oxygen in this case). I have attempted to solve this myself, but I just cannot get Julia to read the output 😕 Here is my (last) attempt:

```julia
b = `go-sendxmpp -f config --listen`
open(b, "w", stdout) do io 
   for ln in readlines(io)
      println(split(ln, " "))
   end
end

```

I have tried many different variations of this, but (if it doesn’t error), it just ignores the Julia code and prints out the full line, e.g.

```julia
2022-07-15T21:51:09+02:00 user@server: hello world!

```

Does anybody know how I can catch the messages one by one?

---

<div class="post-metadata">

### Author: ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)
#### Post date: [July 15, 2022, 9:17pm UTC](https://discourse.julialang.org/t/reading-output-from-external-program-in-stdout/84289/2 "2022-07-15T21:17:16Z")

</div>

> [@Elmo](#):
>
> `open(b, "w", stdout)`

If you’re reading from the program output, it should be

```julia
open(b, "r", stdout)

```

Also, make sure the program does write to `stdout` not `stderr`.

---

<div class="post-metadata">

### Author: ![Elmo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmo/32/17979_2.png) [@Elmo](https://discourse.julialang.org/u/Elmo)
#### Post date: [July 15, 2022, 9:29pm UTC](https://discourse.julialang.org/t/reading-output-from-external-program-in-stdout/84289/3 "2022-07-15T21:29:49Z")

</div>

Ah, okay, I changed it to `open(b, "r", stdout)` but now nothing gets printed when send messages to the account. I confirmed that messages actually come through by running `run(b)` and then it prints to the repl. I am pretty sure it sends to the stdout because the docs say so, but also I changed the IO to stderr and that did not fix it either 😕

---

<div class="post-metadata">

### Author: ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)
#### Post date: [July 15, 2022, 9:37pm UTC](https://discourse.julialang.org/t/reading-output-from-external-program-in-stdout/84289/4 "2022-07-15T21:37:51Z")

</div>

Try change `readlines` to `readline`? The former will not return until reaching `eof` of an IO buffer.

---

<div class="post-metadata">

### Author: ![Elmo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmo/32/17979_2.png) [@Elmo](https://discourse.julialang.org/u/Elmo)
#### Post date: [July 15, 2022, 9:50pm UTC](https://discourse.julialang.org/t/reading-output-from-external-program-in-stdout/84289/5 "2022-07-15T21:50:00Z")

</div>

Awesome! That partially fixes it 🙂 Now I have:

```julia
b = `go-sendxmpp -f config --listen`
open(b, "r", stdout) do io 
   for ln in readline(io)
      println(ln)
   end
end

```

which outputs individual characters for the first message (which is no problem, I can just join the characters if I write messages with a delimiter to separate individual messages from each other):

```julia
2
0
2
2
...
w
o
r
l
d
!

```

However, when I send another message I get this error:

```julia
ERROR: IOError: open(do): broken pipe (EPIPE)
Stacktrace:
 [1] open(::var"#5#6", ::Cmd, ::String, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ Base .\process.jl:404
 [2] open(::Function, ::Cmd, ::String, ::Base.TTY)
   @ Base .\process.jl:393
 [3] top-level scope
   @ REPL[2]:1

```

---

<div class="post-metadata">

### Author: ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)
#### Post date: [July 15, 2022, 9:58pm UTC](https://discourse.julialang.org/t/reading-output-from-external-program-in-stdout/84289/6 "2022-07-15T21:58:57Z")

</div>

This seems to work for me.

```julia
# `b` is some `Cmd` object
open(b, "r", stdout) do io
   while !eof(io)
       println(readuntil(io, '\n'))
   end
end

```

But I don’t understand why the `readline` version didn’t work.

---

<div class="post-metadata">

### Author: ![Elmo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmo/32/17979_2.png) [@Elmo](https://discourse.julialang.org/u/Elmo)
#### Post date: [July 15, 2022, 10:04pm UTC](https://discourse.julialang.org/t/reading-output-from-external-program-in-stdout/84289/7 "2022-07-15T22:04:02Z")

</div>

It works! Thank you very much for helping me, I really appreciate it 🙂

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [July 18, 2022, 4:16pm UTC](https://discourse.julialang.org/t/reading-output-from-external-program-in-stdout/84289/8 "2022-07-18T16:16:31Z")

</div>

> [@greatpet](#):
>
> But I don’t understand why the `readline` version didn’t work.

> [@Elmo](#):
>
> `for ln in readline(io)`

The loop iterates over the `Char`s in the string returned by `readline`. It calls `readline` once when entering the loop.
