# How to attach to an existing remote REPL?

**URL:** <https://discourse.julialang.org/t/how-to-attach-to-an-existing-remote-repl/27608>\
**Category:** Performance\
**Tags:** distributed\
**Created:** [August 16, 2019, 9:46am UTC](https://discourse.julialang.org/t/how-to-attach-to-an-existing-remote-repl/27608 "2019-08-16T09:46:22Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![csavorgn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/csavorgn/32/9798_2.png) [@csavorgn](https://discourse.julialang.org/u/csavorgn)\
**Post date:** [August 16, 2019, 9:46am UTC](https://discourse.julialang.org/t/how-to-attach-to-an-existing-remote-repl/27608/1 "2019-08-16T09:46:22Z")

</div>

Hi,  
In my workflow I need to run several simulations which make use of the DifferentialEquations package.  
To automate running the simulations I need to start the simulations from command line (and not within the Julia REPL):  
julia sim1.jl  
julia sim2.jl  
julia sim3.jl  
etc…  
The problem I’m facing is that the startup time is large and it constitutes the major part of running “julia simX.jl”. Using PackageCompiler to shorten startup time is not an option because I cannot make it work reliably on all the workstations I have to use to run the tests.  
After some thinking I started wondering if it is possible to connect to an existing REPL and run the tests sequentially from my remote connection. I looked at the documentation of the Distributed package and it seems like there is all the stuff I need but I couldn’t figure out how to get what I wanted. What I’d like to achieve is the following (example for 2 simulations)

1. connect to the REPL
2. run the simulation 1
3. disconnect from the REPL. The REPL continues to exist.
4. connect to the same REPL
5. run the simulation 2
6. disconnect from the REPL.

Is it possible to attach the current julia process to an existing REPL (remote or on the same computer)?  
If it is not possible, what can I do to speed up my work?

Thank you  
Carlo

---

<div class="post-metadata">

**Author:** ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)\
**Post date:** [August 16, 2019, 10:33am UTC](https://discourse.julialang.org/t/how-to-attach-to-an-existing-remote-repl/27608/2 "2019-08-16T10:33:18Z")

</div>

Carlo, this is NOT a direct answer to your question. An expert will come along in a minute.

If you use an ssh connection to a remote system you can use a utility called ‘screen’  
A screen session will keep an application running and you can disconnect and reconnect to the terminal.

> **[How To Use Linux Screen](https://www.rackaid.com/blog/linux-screen-tutorial-and-how-to/)**
>
> Quicky learn how to use Linux screen in this two minute video. Learn how to run multiple, persistent shell windows from a single network connection.

---

<div class="post-metadata">

**Author:** ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)\
**Post date:** [August 16, 2019, 10:35am UTC](https://discourse.julialang.org/t/how-to-attach-to-an-existing-remote-repl/27608/3 "2019-08-16T10:35:50Z")

</div>

ALso - if you have a REPL session running you can avoid stopping it and restarting.  
Look at the Revise package by Tim Holy

> **[GitHub - timholy/Revise.jl: Automatically update function definitions in a...](https://github.com/timholy/Revise.jl)**
>
> Automatically update function definitions in a running Julia session - GitHub - timholy/Revise.jl: Automatically update function definitions in a running Julia session

[![](https://global.discourse-cdn.com/julialang/original/3X/3/8/384a1e392db9f0ee8eeb760574a496e9633d2e3b.jpeg "JuliaCon 2018 | Making the test-debug cycle more efficient | Tim Holy") ](https://www.youtube.com/watch?v=KuM0AGaN09s)

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [August 16, 2019, 11:18am UTC](https://discourse.julialang.org/t/how-to-attach-to-an-existing-remote-repl/27608/4 "2019-08-16T11:18:49Z")

</div>

Have you considered using a jupyter notebook?  
That should allow using a remote julia session (which stays alive)  
Or is this not an option for you?  
You can check this out on juliabox  
Apologies if I misunderstood your problem.

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [August 16, 2019, 11:27am UTC](https://discourse.julialang.org/t/how-to-attach-to-an-existing-remote-repl/27608/5 "2019-08-16T11:27:36Z")

</div>

`screen` or `tmux` are the canonical solutions to this problem.  
[Juno](http://docs.junolab.org/latest/man/faq/#Connecting-to-a-Julia-session-on-a-remote-machine-1) can automate that process for you pretty well, if you want to give it a go.

---

<div class="post-metadata">

**Author:** ![csavorgn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/csavorgn/32/9798_2.png) [@csavorgn](https://discourse.julialang.org/u/csavorgn)\
**Post date:** [August 16, 2019, 11:50am UTC](https://discourse.julialang.org/t/how-to-attach-to-an-existing-remote-repl/27608/6 "2019-08-16T11:50:08Z")

</div>

Thank you for the suggestion but I really need to start my simulations from the command line. Carlo

---

<div class="post-metadata">

**Author:** ![csavorgn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/csavorgn/32/9798_2.png) [@csavorgn](https://discourse.julialang.org/u/csavorgn)\
**Post date:** [August 16, 2019, 11:51am UTC](https://discourse.julialang.org/t/how-to-attach-to-an-existing-remote-repl/27608/7 "2019-08-16T11:51:04Z")

</div>

Actually I was planning to use Revise if I manage to get this working. Thank you  
Carlo

---

<div class="post-metadata">

**Author:** ![csavorgn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/csavorgn/32/9798_2.png) [@csavorgn](https://discourse.julialang.org/u/csavorgn)\
**Post date:** [August 16, 2019, 11:56am UTC](https://discourse.julialang.org/t/how-to-attach-to-an-existing-remote-repl/27608/8 "2019-08-16T11:56:27Z")

</div>

@johnh and @pfitzseb  
I knew about screen but it didn’t came into my mind to solve the problem. I think with screen I could get what I want but if I could attach and detach to a REPL using the same tools used for distributed computing I could also get the results of my simulations which could help me understand if everything went ok or not. Probably I can do it also with screen but it would be more complicated.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [August 16, 2019, 12:46pm UTC](https://discourse.julialang.org/t/how-to-attach-to-an-existing-remote-repl/27608/9 "2019-08-16T12:46:48Z")

</div>

Attaching a REPL to a running remote process is in principle possible but no one has done the work to make it happen. Feel free to open a feature request issue on GitHub. Note that there are pretty hefty security implications of allowing such a thing, so it’s not just a matter of making it work, but also of making it work safely.

---

<div class="post-metadata">

**Author:** ![csavorgn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/csavorgn/32/9798_2.png) [@csavorgn](https://discourse.julialang.org/u/csavorgn)\
**Post date:** [August 16, 2019, 1:10pm UTC](https://discourse.julialang.org/t/how-to-attach-to-an-existing-remote-repl/27608/10 "2019-08-16T13:10:56Z")

</div>

Thank you for the feedback. Would it be a quite unsafe thing also using the Distributed.jl package infrastructure? I don’t see a big difference between attaching to a running Julia process and distributed computing with Julia.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [August 16, 2019, 1:47pm UTC](https://discourse.julialang.org/t/how-to-attach-to-an-existing-remote-repl/27608/11 "2019-08-16T13:47:30Z")

</div>

True, being able to start a REPL on any worker would be safe and probably easier to implement since the “RPC” already just works.

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [August 22, 2019, 7:25am UTC](https://discourse.julialang.org/t/how-to-attach-to-an-existing-remote-repl/27608/12 "2019-08-22T07:25:36Z")

</div>

For server-side long-running applications (everything web dev) this would be a very valuable feature. For reference, it is available on Erlang/Elixir. The value resides in the fact that  
a. one can attach to a running session to debug issues and inspect application state  
b. we could theoretically update the running code by manually triggering Revise to inject application updates without restarting the app (no downtime)

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [August 22, 2019, 7:58am UTC](https://discourse.julialang.org/t/how-to-attach-to-an-existing-remote-repl/27608/13 "2019-08-22T07:58:27Z")

</div>

If you start remote IJulia via [`remote_ikernel`](https://bitbucket.org/tdaff/remote_ikernel/src/default/) you can attach as many local clients as you want. The catch is that you have to start IJulia via `remote_ikernel` and loosing connection will kill the remote process. I’ve been wanting to write a “Jupyter kernel proxy” that can bridge the pre-existing remote Jupyter kernel and local client though SSH. This would help not only Julia community but also entire scientific computing community.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 22, 2019, 8:27am UTC](https://discourse.julialang.org/t/how-to-attach-to-an-existing-remote-repl/27608/14 "2019-08-22T08:27:05Z")

</div>

> [@csavorgn](#):
>
> Probably I can do it also with screen but it would be more complicated.

FWIW, I am using `ssh` + `tmux` on our computational server. It is robust and extremely convenient.

I exchange data and results back and forth using `rsync` or a private repo on Gitlab.

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [August 29, 2019, 8:14am UTC](https://discourse.julialang.org/t/how-to-attach-to-an-existing-remote-repl/27608/15 "2019-08-29T08:14:35Z")

</div>

+1 for tmux, I’ve also used it with Julia apps started from the REPL. The downside is tmux has a pretty high learning curve (and I still can’t remember the shortcuts, I’m having all these tmux cheat-sheets around). Will give `screen` a try, seems more user friendly.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 29, 2019, 9:08am UTC](https://discourse.julialang.org/t/how-to-attach-to-an-existing-remote-repl/27608/16 "2019-08-29T09:08:52Z")

</div>

> [@essenciary](#):
>
> tmux has a pretty high learning curve (and I still can’t remember the shortcuts

I use basically 3-4 of them, for creating a new terminal, stepping back and forth between them, and killing an unresponsive one.

No doubt there must be a lot of excellent features I am missing with this approach, but I don’t spend enough time in a terminal to make learning all of them worthwhile.

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [August 30, 2019, 7:43am UTC](https://discourse.julialang.org/t/how-to-attach-to-an-existing-remote-repl/27608/17 "2019-08-30T07:43:07Z")

</div>

A quick follow up based on my current struggles at work:

1 - `tmux` / `screen` works in some cases, but in production, most certainly, we need the web apps demonized so that they’re automatically restarted upon crashing. This means that the Julia process will be started by something like `supervisord` not by the user, so we need to attach to _that_ process.

2 - found `byobu` which is a more user friendly wrapper around `tmux` and `screen`

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [August 30, 2019, 7:53am UTC](https://discourse.julialang.org/t/how-to-attach-to-an-existing-remote-repl/27608/18 "2019-08-30T07:53:22Z")

</div>

> [@essenciary](#):
>
> so we need to attach to _that_ process

How about launching a IJulia kernel and then use `@spwan`/`@async` to start your webapp inside of it? You can then attach a REPL to it using `jupyter console` etc.

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [August 30, 2019, 8:22am UTC](https://discourse.julialang.org/t/how-to-attach-to-an-existing-remote-repl/27608/19 "2019-08-30T08:22:45Z")

</div>

Interesting! I have 0 knowledge of IJulia’s internals - would that carry a significant performance penalty?

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [August 30, 2019, 8:42am UTC](https://discourse.julialang.org/t/how-to-attach-to-an-existing-remote-repl/27608/20 "2019-08-30T08:42:34Z")

</div>

I don’t think you need to know IJulia internal to use it. There is no direct API to do it but I think you can put something like `@spawn start_my_app()` in `~/.julia/config/startup_ijulia.jl`. Yeah, I know it’s a bit of a dirty trick but maybe a good start for trying things out. Guarding it with some environment variable (which is set in `supervisord` configuration) probably is a good idea. So, the invocation you need to use (e.g., put in `supervisord` configuration) would be something like `START_MY_APP=yes jupyter console --kernel=julia-1.x`. To attach to it, use `jupyter console --existing KERNEL_ID`. See: [Jupyter console 6.0 — Jupyter console 6.0.0dev documentation](https://jupyter-console.readthedocs.io/en/latest/)

> [@essenciary](#):
>
> would that carry a significant performance penalty?

Sorry, I’ve never tried this (IJulia + web app) myself so don’t know. I know that a Jupyter kernel needs to open some ZMQ connections (which uses a bunch of TCP ports) to talk with Jupyter frontends. Maybe this can interfere with I/O performance of your web app? For CPU-bound task, my guess is that it won’t matter since people do computations in Jupyter notebooks and they’d complain if there is any performance disadvantage.

[Next page](https://discourse.julialang.org/t/how-to-attach-to-an-existing-remote-repl/27608.md?page=2)
