# Programmatically restart julia

**URL:** <https://discourse.julialang.org/t/programmatically-restart-julia/73806>\
**Category:** General Usage\
**Created:** [December 30, 2021, 9:03am UTC](https://discourse.julialang.org/t/programmatically-restart-julia/73806 "2021-12-30T09:03:43Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [December 30, 2021, 9:03am UTC](https://discourse.julialang.org/t/programmatically-restart-julia/73806/1 "2021-12-30T09:03:43Z")

</div>

I am creating a set of independent scripts for illustrating some concepts and I want to be sure that each of them starts “clean”.  
Performance is not a matter, how can I put something at the beginning of each script to force restart of the julia process ?

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [December 30, 2021, 9:14am UTC](https://discourse.julialang.org/t/programmatically-restart-julia/73806/2 "2021-12-30T09:14:05Z")

</div>

I have tested this code:

```julia
function workspace()
    atexit() do
        run(`$(Base.julia_cmd())`)
    end
    exit()
 end
 workspace()

```

However while it works on the REPL, it freeze on VSCode…

This one seems to work but it actually doesn’t:

```julia
run(`$(Base.julia_cmd()) -e "println(\"Starting this script from a new, clean session\")"`) 

```

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [December 30, 2021, 10:56am UTC](https://discourse.julialang.org/t/programmatically-restart-julia/73806/3 "2021-12-30T10:56:33Z")

</div>

Turn your scripts into functions (or modules). That way they get their own namespace, which should solve any problem you have from an unclean environment, as well as improve performance generally.

If they are truly independent scripts, and you truly don’t care how long they take to run, you could run them through `julia filename.jl` from the terminal.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [December 30, 2021, 11:00am UTC](https://discourse.julialang.org/t/programmatically-restart-julia/73806/4 "2021-12-30T11:00:54Z")

</div>

Yes, this is the way I use for running the documentation of my packages in github CI, but here I have students that need to manually run these scripts and I could just say them “run this script in a new Julia session” but I would like to have something on the top of the script they can click and run, even if they don’t get the full meaning, but I am sure then they don’t have garbage left from previous lessons (scripts)…

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [December 30, 2021, 11:06am UTC](https://discourse.julialang.org/t/programmatically-restart-julia/73806/5 "2021-12-30T11:06:01Z")

</div>

How about “Call this function”?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 17, 2023, 5:30pm UTC](https://discourse.julialang.org/t/programmatically-restart-julia/73806/6 "2023-01-17T17:30:11Z")

</div>

FYI, a more complete function by @fredrikekre is [available here](https://github.com/JuliaLang/julia/issues/32129#issue-448121208), and it is called `restart()`.

I added it to the `startup.jl` file and tested it in VS Code and in a standalone Julia REPL and it seems to work very well.

For the latter, it restarts the REPL in the same window very quickly in Julia 1.9 and thus, it might be seen as a “clear workspace” analogue.

---

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [January 17, 2023, 7:42pm UTC](https://discourse.julialang.org/t/programmatically-restart-julia/73806/7 "2023-01-17T19:42:40Z")

</div>

I am using `restart()` all the time. Works great.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [January 17, 2023, 10:16pm UTC](https://discourse.julialang.org/t/programmatically-restart-julia/73806/8 "2023-01-17T22:16:22Z")

</div>

> [@rafael.guerra](#):
>
> FYI, a more complete function by @fredrikekre is [available here](https://github.com/JuliaLang/julia/issues/32129#issue-448121208), and it is called `restart()`.

An improved version of that, which uses [execv](https://linux.die.net/man/3/execv) to replace the current process (instead of nesting processes as in the `restart()` function) can be found here: [fredrikekre/.dotfiles/.julia/config/startup.jl](https://github.com/fredrikekre/.dotfiles/blob/e0e46a7a1ac3acb4d3c1fe722e5097292b23222b/.julia/config/startup.jl#L12-L21). I have this mapped to `:r` in the REPL, but you can of course take that and define a `restart()` function.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 17, 2023, 10:39pm UTC](https://discourse.julialang.org/t/programmatically-restart-julia/73806/9 "2023-01-17T22:39:06Z")

</div>

Hi @fredrikekre, thanks for linking your improved code, which is totally beyond me. The `:r` restart sounds great, but how do we include the relevant code in our startup.jl? And optionally, how could we also convert it to a simple `restart()` function? Thank you.

PS: and will **execv** work in Windows 11?

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [January 17, 2023, 10:43pm UTC](https://discourse.julialang.org/t/programmatically-restart-julia/73806/10 "2023-01-17T22:43:31Z")

</div>

Wrapping the highlighted code in a function seems to just work:

```julia
 function restart()
   argv = Base.julia_cmd().exec
   opts = Base.JLOptions()
   if opts.project != C_NULL
       push!(argv, "--project=$(unsafe_string(opts.project))")
   end
   if opts.nthreads != 0
       push!(argv, "--threads=$(opts.nthreads)")
   end
   @ccall execv(argv[1]::Cstring, argv::Ref{Cstring})::Cint
end

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 17, 2023, 10:50pm UTC](https://discourse.julialang.org/t/programmatically-restart-julia/73806/11 "2023-01-17T22:50:24Z")

</div>

@aramirezreyes, on Windows 11, Julia 1.9.0-beta2, I get:

```julia
julia> restart()
ERROR: could not load symbol "execv":
The specified procedure could not be found.

```

Tried `_execv` but it does not launch Julia back.
