# Buffer in running external program in loops

**URL:** <https://discourse.julialang.org/t/buffer-in-running-external-program-in-loops/78087>\
**Category:** Performance\
**Tags:** question, run\
**Created:** [March 18, 2022, 2:02pm UTC](https://discourse.julialang.org/t/buffer-in-running-external-program-in-loops/78087 "2022-03-18T14:02:25Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [March 18, 2022, 3:12pm UTC](https://discourse.julialang.org/t/buffer-in-running-external-program-in-loops/78087/21 "2022-03-18T15:12:23Z")

</div>

> [@ValentinChb](#):
>
> ```julia
> run(` cmd start /c script.bat `)
> 
> ```

This would be wrong, right is:

```julia
run(` cmd /c "start script.bat" `)

```

You can use triple back ticks:  
```  
code  
```

---

<div class="post-metadata">

**Author:** ![ValentinChb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/valentinchb/32/34668_2.png) [@ValentinChb](https://discourse.julialang.org/u/ValentinChb)\
**Post date:** [March 18, 2022, 3:16pm UTC](https://discourse.julialang.org/t/buffer-in-running-external-program-in-loops/78087/23 "2022-03-18T15:16:14Z")

</div>

I mean, nothing happens and it lands in a deadlock. When I abort, the external program corresponding to `ic=1` is launched in a new window.

EDIT: I reached the limit of number of posts for a new user (I know I should have created an account way earlier). Any way to avoid waiting 22 hours?

I’ll keep on editing here. In the REPL, it fails to run without any message, stg I often get when Julia meets an unexpected error it cannot handle. EDIT in EDIT: sorry, that’s when I use the shortcut “execute code and move” in VSCode, if I use include(“mycode.jl”) it leads to a deadlock as in the shell, but I can’t abort without stopping the REPL so I don’t know if the external program would ever be run.

EDIT 2: script.bat is in the current folder, I’m using

```julia-auto
joinpath(@ __DIR__ ,"script.bat") 

```

to get its absolute path.

EDIT 3: I’m working on a MWE and will keep you updated!

> [@oheil](#):
>
> ```julia-auto
> run(`PATH\TO\script.bat`)
> 
> ```

without anything else is not possible, I need arguments in practice…

EDIT 4: Problem solved, but not fully understood.

First, I feel sorry to have forgotten an important detail: I was not using

```julia-auto
run(command)

```

but

```julia-auto
try @spawnat :any run(command) catch; run(command) end

```

using non-necessary multi-threading (remnant of an earlier version to provide parallel execution for julia -and not system- code).

The problem disappeared by removing the multi-threading stuff, but why??? I could not reproduce this on my MWE (that is similar to yours). There, both the single- and multi-threaded versions work. So I dug a bit further and yes, your intuition was right, in the end the simple

```julia-auto
run(`cmd /c "start $myexe $myinput"`)

```

just works fine. Basically I had the `/c` on the wrong side of the `start` in the `run` statement, and had to compensate by using an extra script implementing yet another `start`. Still what multi-threading has to do with this is a mystery we don’t need to solve. Thanks for your time and wisdom!!!

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [March 18, 2022, 3:18pm UTC](https://discourse.julialang.org/t/buffer-in-running-external-program-in-loops/78087/24 "2022-03-18T15:18:15Z")

</div>

> [@ValentinChb](#):
>
> Thanks.
> 
> ```julia
> run(`cmd /c "start script.bat"`)
> 
> ```
> 
> gives the same result.

What happens if you do just this in a REPL? Is it working than?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [March 18, 2022, 3:31pm UTC](https://discourse.julialang.org/t/buffer-in-running-external-program-in-loops/78087/25 "2022-03-18T15:31:47Z")

</div>

> [@ValentinChb](#):
>
> In the REPL, it fails to run without any message

Is script.bat in your path or in the current folder?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [March 18, 2022, 3:39pm UTC](https://discourse.julialang.org/t/buffer-in-running-external-program-in-loops/78087/26 "2022-03-18T15:39:37Z")

</div>

I need to see your code.  
I suspect, that you are creating your commands using Strings and than you probably do the

```julia-auto
`...` 

```

or the

```julia-auto
Cmd(`...`) 

```

wrong.  
Please try to create a MWE which reproduces your issue without being overly complex. My guess is, while you do this you will solve your issue by your own 😉  
MWE = minimal working example ([Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757))

> [@oheil](#):
>
> ```julia-auto
> run(`cmd start script.bat`)
> 
> ```

And I am pretty sure, that this creates an issue.  
What is done with that is:  
Julia `exec` a `cmd.exe` with `ARGV[1]="start"` and `ARGV[2]="script.bat"`.  
So cmd.exe will start interactive, runs `start` without parameter which fails. Now cmd as an interactive shell stalls whatever is expected to run now.  
I think it would be best to just use

```julia-auto
run(`PATH\TO\script.bat`)

```

without anything else. Or if this is not possible:

```julia-auto
run(`cmd /C "start script.bat"`)

```

The " are important here, because  
`ARGV[1]` must be `/C` and  
`ARGV[2]` must be the whole “`start script.bat`”

You may be able to post again, thanx to @mbauman

[Previous page](https://discourse.julialang.org/t/buffer-in-running-external-program-in-loops/78087.md?page=1)
