Programmatically restart julia

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 ?

I have tested this code:

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:

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

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.

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)…

How about “Call this function”?

FYI, a more complete function by @fredrikekre is available here, 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.

2 Likes

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

2 Likes

An improved version of that, which uses 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. I have this mapped to :r in the REPL, but you can of course take that and define a restart() function.

2 Likes

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?

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

 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
3 Likes

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

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

Tried _execv but it does not launch Julia back.