2

Can I create one instance of Julia and use it to run multiple Julia scripts from bash script?

#!/bin/bash
J=getjuliainstance()
J.run(temp.jl)
J.run(j1.jl)
J.run(j2.jl)
J.run(j3.jl)
J.exit()

I could run all of them from inside a master Julia script but that is not the intent.

This is to circumvent Julia's load time for the first script so that runtime of subsequent scripts can be timed consistently.

Any way to spawn a single process and reuse it to launch scripts? From shell script only please!

One of the solutions (allows for a tail -f):

julia <pipe 2>&1 | tee submission.log > /dev/null &
Tims
  • 627
  • 7
  • 19
  • I don't understand what the problem with a master Julia script is...every command you run from a shell script will be a separate instance. – Tom Fenech Jan 27 '18 at 15:27

1 Answers1

4

You can try named pipes:

$ mkfifo pipe # create named pipe

$ sleep 10000 > pipe & # keep pipe alive
[1] 11521

$ julia -i <pipe & # make Julia read from pipe
[2] 11546

$ echo "1+2" >pipe

$ 3


$ echo "rand(10)" >pipe

$ 10-element Array{Float64,1}:
 0.938396
 0.690747
 0.615235
 0.298277
 0.780966
 0.775423
 0.197329
 0.136582
 0.302169
 0.607562


$

You can send any commands to Julia using echo. If you use stdout for Julia output then you have to press enter when Julia writes something there to return to prompt. Stop Julia by writing echo "exit()" >pipe. If you want to execute a file this way use include function.

EDIT: it seems that you even do not have to use -i if you run Julia this way.

EDIT2: I did not notice that actually you want to use only one bash script (not an interactive mode). In such a case it should be even simpler to use named pipes.

Bogumił Kamiński
  • 66,844
  • 3
  • 80
  • 107
  • Thanks Bogumit, will try and get back to you – Tims Jan 27 '18 at 17:25
  • seems to hang if i don't exit() within sleep time. how can i keep that in check? – Tims Jan 28 '18 at 01:06
  • Julia is kept alive until it receives EOF from `pipe` or `exit()` is sent. So it does not hang but it terminates. Using `sleep` is a hack that makes `echo` not send EOF on the first run. Notice that if you `kill` the `sleep` process before it finishes then `julia` process will terminate also. The simple solution is to increase time in `sleep` to a very large value or to write a script wrapping `sleep` in an infinite `while true; do` loop and running this script. – Bogumił Kamiński Jan 28 '18 at 08:35
  • the thing is I am running someone else's Julia code through "include" that I don't have direct control of. I was using linux timeout command to force exit like timeout 100000 xyz.jl. what would be a good equivalent? – Tims Jan 29 '18 at 17:10
  • while true end runs in an infinite loop but when I say echo "while true end" > pipe, it returns immediately why? – Tims Jan 29 '18 at 17:28
  • can the julia script generate files on the disk when I say echo "include(...)" > pipe? – Tims Jan 29 '18 at 18:03
  • if you want to Julia to write stdout/stderr to a disk you have to declare it as an additional redirect when Julia is started with `julia – Bogumił Kamiński Jan 29 '18 at 18:09
  • I am running someone else's code using include. I have no control what disk files they are creating,...but I would like to allow them to create files..How can I let that happen? – Tims Jan 29 '18 at 18:28
  • You are asking about several different things in your questions. It is hard to clearly understand what you need. Maybe open other question for specific other issues? As for writing to disk - `include(...)` will allow an external process to write to disk if it has write permission. What I said is that `julia out.txt` will read commands from `pipe` and write stdout to `out.txt`. – Bogumił Kamiński Jan 29 '18 at 19:35
  • Awesome! Thanks! – Balinus Mar 14 '19 at 18:56