# How to exit a Julia program without completely exit REPL or shutting down Julia?

**URL:** https://discourse.julialang.org/t/how-to-exit-a-julia-program-without-completely-exit-repl-or-shutting-down-julia/66655
**Category:** General Usage
**Created:** [August 19, 2021, 5:46am UTC](https://discourse.julialang.org/t/how-to-exit-a-julia-program-without-completely-exit-repl-or-shutting-down-julia/66655 "2021-08-19T05:46:01Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![CRquantum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crquantum/32/27824_2.png) [@CRquantum](https://discourse.julialang.org/u/CRquantum)
#### Post date: [August 19, 2021, 5:46am UTC](https://discourse.julialang.org/t/how-to-exit-a-julia-program-without-completely-exit-repl-or-shutting-down-julia/66655/1 "2021-08-19T05:46:01Z")

</div>

Sorry for another stupid question.  
In a Julia program, or a function, if some ‘Oops’ happens, I know that I can put

```
exit()

```

somewhere in the code, so that I can automatically and completely shutting down julia and quit.

I mean like, in the windows CMD, doing exit() it will completely quit Julia. In Jupyter Notebook, doing exit() will shutting down the Julia kernel.  
Like, once Julia is in the environment variable, in CMD I can type julia and enter REPL,

```
      C:\Users\Trump>julia
                     _
         _ _ _(_)_ | Documentation: https://docs.julialang.org
        (_) | (_) (_) |
         _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
        | | | | | | |/ _` | |
        | | |_| | | | (_| | | Version 1.5.4 (2021-03-11)
       _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
      |__/ |

```

Now if I type, exit(), it just complete quit,

```
julia> exit()
C:\Users\Trump>

```

Is there a way to quit however still in the REPL environment? Like

```
julia> somecommand()
julia>

```

I know there are some other guys might have asked similar question, but the answer is not very clear.

1. I just wonder, in Julia, is there a command such that one can quit, but do not completely shutting down Julia?

Or is it by design that Julia only allow a complete exit?  
However, this is also reasonable, because if it is a complete exit, all the workspace will be cleaned, and this is actually reasonable.

1. Is there a command which can completely clean the workspace (remove all the variables, definitions of functions, just delete everything) without quit the REPL?  
I mean like, just say in Jupyter Notebook, currently if I want to completely clean the workspace, I need to restart the kernal. Although restarting the kernal is fast, I believe there might be smarter ways to clean the workspace.

Thanks in advance!

---

<div class="post-metadata">

### Author: ![MisterBiggs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/misterbiggs/32/30722_2.png) [@MisterBiggs](https://discourse.julialang.org/u/MisterBiggs)
#### Post date: [August 19, 2021, 6:26am UTC](https://discourse.julialang.org/t/how-to-exit-a-julia-program-without-completely-exit-repl-or-shutting-down-julia/66655/2 "2021-08-19T06:26:36Z")

</div>

I believe that running Julia again from the REPL does what you are looking for:

```julia
run(`julia.exe`)

```

Image example aswell:

![image](https://global.discourse-cdn.com/julialang/original/3X/2/e/2ed4885246b15e778a35b07faf7ae8d400ec03ea.png)

Not sure if it works in jupyter since I don’t have it installed but it works for me in the REPL.

---

<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 19, 2021, 6:38am UTC](https://discourse.julialang.org/t/how-to-exit-a-julia-program-without-completely-exit-repl-or-shutting-down-julia/66655/3 "2021-08-19T06:38:21Z")

</div>

Perhaps Tim Holys Revise.jl can help in your workflow?

> **[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

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [August 19, 2021, 6:39am UTC](https://discourse.julialang.org/t/how-to-exit-a-julia-program-without-completely-exit-repl-or-shutting-down-julia/66655/4 "2021-08-19T06:39:07Z")

</div>

> [@CRquantum](#):
>
> I just wonder, in Julia, is there a command such that one can quit, but do not completely shutting down Julia?

That’s typically done by throwing an exception (`error("oh no!")`).

> [@CRquantum](#):
>
> Is there a command which can completely clean the workspace (remove all the variables, definitions of functions, just delete everything) without quit the REPL?

At the moment no. There used to be a `workspace()` command that did this but it had some issues so it was removed. However, I think the underlying issues have been fixed so it is possible that could be put back.

---

<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: [August 19, 2021, 12:33pm UTC](https://discourse.julialang.org/t/how-to-exit-a-julia-program-without-completely-exit-repl-or-shutting-down-julia/66655/5 "2021-08-19T12:33:48Z")

</div>

> [@kristoffer.carlsson](#):
>
> At the moment no. There used to be a `workspace()` command that did this but it had some issues so it was removed. However, I think the underlying issues have been fixed so it is possible that could be put back.

I think you refer to pre Julia-1…  
Having back a `workspace()` function would really be useful… I saw this request asked tens of time by users 🙂

---

<div class="post-metadata">

### Author: ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)
#### Post date: [August 19, 2021, 2:04pm UTC](https://discourse.julialang.org/t/how-to-exit-a-julia-program-without-completely-exit-repl-or-shutting-down-julia/66655/6 "2021-08-19T14:04:51Z")

</div>

Even more powerful than a `workspace` command would be some kind of “change module” command that would cause subsequent REPL inputs to be evaluated in a module different from `Main`. Imagine if one could do something like:

```julia
julia> module Main2 end; # create a new, empty workspace

julia> change_module(Main2) # Move the REPL to that workspace

julia/Main2> data = Main.data; # Stuff from old workspace is still accessible.

```

---

<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 19, 2021, 2:15pm UTC](https://discourse.julialang.org/t/how-to-exit-a-julia-program-without-completely-exit-repl-or-shutting-down-julia/66655/7 "2021-08-19T14:15:07Z")

</div>

[https://gist.github.com/pfitzseb/8fab21f09d6c15246d4a0960af3c65b1](https://gist.github.com/pfitzseb/8fab21f09d6c15246d4a0960af3c65b1)

---

<div class="post-metadata">

### Author: ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)
#### Post date: [August 19, 2021, 5:03pm UTC](https://discourse.julialang.org/t/how-to-exit-a-julia-program-without-completely-exit-repl-or-shutting-down-julia/66655/8 "2021-08-19T17:03:36Z")

</div>

> [@Per](#):
>
> some kind of “change module” command that would cause subsequent REPL inputs to be evaluated in a module different from `Main`

There is [PR](https://github.com/JuliaLang/julia/pull/33872) doing that, but it needs some more work.

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [August 19, 2021, 5:11pm UTC](https://discourse.julialang.org/t/how-to-exit-a-julia-program-without-completely-exit-repl-or-shutting-down-julia/66655/9 "2021-08-19T17:11:34Z")

</div>

Is there an equivalent of the “exec” functions from C? so you could just tell Julia to exec Julia. This is equivalent really to `exit()` followed by `julia` except you can do it from entirely within Julia. It has the advantage of not leaving any threads or tasks running in the background, and unlike run() doesn’t leave the original Julia running.

If not, perhaps someone could just create Exec.jl which uses ccall to provide them?

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [August 19, 2021, 5:12pm UTC](https://discourse.julialang.org/t/how-to-exit-a-julia-program-without-completely-exit-repl-or-shutting-down-julia/66655/10 "2021-08-19T17:12:46Z")

</div>

> [@MisterBiggs](#):
>
> I believe that running Julia again from the REPL does what you are looking for:
> 
> ```julia
> run(`julia.exe`)
> 
> ```

this does that. Also C is not interactive, what’s the point

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [August 19, 2021, 5:16pm UTC](https://discourse.julialang.org/t/how-to-exit-a-julia-program-without-completely-exit-repl-or-shutting-down-julia/66655/11 "2021-08-19T17:16:54Z")

</div>

if I understand the help on run(), `run("julia.exe")` causes the running Julia to run a sub-process and wait for it to exit. This means you’ve now got two processes… suppose the first process has enormous quantities of memory allocated for example, they still are allocated… the new Julia process is clean, but the old one is still hanging out there… If you do run() again you’ll have 3 processes, and then 4, and then 5 etc…

`exec` causes the current process to be _replaced_ by a process running the new command. So now every time you run `exec("julia")` you don’t get a _new_ process, you just completely reset the process to be a fresh run of julia.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [August 19, 2021, 5:19pm UTC](https://discourse.julialang.org/t/how-to-exit-a-julia-program-without-completely-exit-repl-or-shutting-down-julia/66655/12 "2021-08-19T17:19:33Z")

</div>

> [@dlakelan](#):
>
> causes the running Julia to run a sub-process and wait for it to exit.

the idea is that after you `Ctrl-C` the sub-process, you are still in Julia (although pretty point less)

> [@dlakelan](#):
>
> be _replaced_ by a process running the new command

ah, I see, thanks for the clarification.

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [August 19, 2021, 5:38pm UTC](https://discourse.julialang.org/t/how-to-exit-a-julia-program-without-completely-exit-repl-or-shutting-down-julia/66655/13 "2021-08-19T17:38:41Z")

</div>

I mean, it would be pretty trivial to provide this through ccall right? I imagine I could do it this afternoon if I took the time to read up on ccall. But it would be better to be a package kind of maintained by some github organization, like JuliaStrings etc except with a “OS interaction” theme or something.

I just tested this on my computer, and it worked:

```julia
julia> ccall(:execvp,Int32,(Cstring,Array{Cstring}),"/home/dlakelan/julia-latest/bin/julia",Cstring[])

```

Also this:

```julia
julia> ccall(:execvp,Int32,(Cstring,Array{Cstring}),"julia",Cstring[])

```

since `execvp` searches the path in the same way the shell does and my personal julia install is in my path 😉

it should also be possible to create a function like this which just re-execs julia with the same arguments (figured out the specific sauce required).

```julia
julia> function reexec()
                 cmd = Base.julia_cmd()
                 @ccall execvp(cmd.exec[1]::Cstring,cmd.exec[2:end]::Ptr{Cstring})::Int32
       end
reexec (generic function with 1 method)

julia> reexec()
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.7.0-beta3.0 (2021-07-07)
 _/ |\ __'_|_|_|\__'_| | release-1.7/e76c9dad42 (fork: 68 commits, 71 days)
|__/ |

```

---

<div class="post-metadata">

### Author: ![CRquantum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crquantum/32/27824_2.png) [@CRquantum](https://discourse.julialang.org/u/CRquantum)
#### Post date: [August 20, 2021, 6:58am UTC](https://discourse.julialang.org/t/how-to-exit-a-julia-program-without-completely-exit-repl-or-shutting-down-julia/66655/14 "2021-08-20T06:58:45Z")

</div>

Thank you very much!  
My another stupid question. So like, if I have a main program called A.jl .  
Now, currently if I want to completely clean the workspace and run it again. What I do is type :

```
exit()

```

then

```
 julia 

```

then

```
include("A.jl")

```

Like

```
julia> exit()

C:\Users\Trump>julia
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.6.1 (2021-04-23)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> include("A.jl")

```

Every time I have to loop over these stupid three steps again and again, I am sure what I did is among the most stupid ways.

May I ask, what do you guys do if you want to completely clean the workspace and run the Julia program again?

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [August 20, 2021, 7:27am UTC](https://discourse.julialang.org/t/how-to-exit-a-julia-program-without-completely-exit-repl-or-shutting-down-julia/66655/15 "2021-08-20T07:27:38Z")

</div>

use DaemonModes maybe? I think it handles that for you by wrapping code inside module.

but why not just return? don’t call exit()

---

<div class="post-metadata">

### Author: ![pixel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel/32/26437_2.png) [@pixel](https://discourse.julialang.org/u/pixel)
#### Post date: [August 20, 2021, 7:45am UTC](https://discourse.julialang.org/t/how-to-exit-a-julia-program-without-completely-exit-repl-or-shutting-down-julia/66655/16 "2021-08-20T07:45:06Z")

</div>

Inside my .julia/config/startup.jl file I have the function

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

```

so whenever i call `clean()` inside a julia session the session will exit and automatically restart. I use this for cleaning a workspace.

---

<div class="post-metadata">

### Author: ![mgkuhn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mgkuhn/32/6276_2.png) [@mgkuhn](https://discourse.julialang.org/u/mgkuhn)
#### Post date: [August 24, 2021, 3:43pm UTC](https://discourse.julialang.org/t/how-to-exit-a-julia-program-without-completely-exit-repl-or-shutting-down-julia/66655/17 "2021-08-24T15:43:30Z")

</div>

You are still creating a second Julia process as a child process of the first Julia process, i.e. the memory and other resources of the first Julia process are still allocated until the second Julia process terminates, as you can see with `ps uf`:

```julia
$ ps f
 188560 pts/7 Ss 0:00 bash
 188574 pts/7 Sl+ 0:00 \_ julia
 188654 pts/7 Sl+ 0:00 \_ julia

```

Instead of `run` you want to use the `execve` function on POSIX systems and the equivatent `_wexecve` function on Win32 systems. These really replace the first process with the second (while keeping the Process ID), i.e. all the memory of the the first process is deallocated and replaced with memory allocated to the newly started executable. You cannot return from an `execve` call. I don’t think Julia Base currently has an `exec` function that works like `run` without ever returning, so you would have to use equivalent platform-specific `ccall` invocations.

---

<div class="post-metadata">

### Author: ![mgkuhn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mgkuhn/32/6276_2.png) [@mgkuhn](https://discourse.julialang.org/u/mgkuhn)
#### Post date: [August 24, 2021, 3:50pm UTC](https://discourse.julialang.org/t/how-to-exit-a-julia-program-without-completely-exit-repl-or-shutting-down-julia/66655/18 "2021-08-24T15:50:15Z")

</div>

If I regularly need to restart Julia after it has run `A.jl`, e.g., because of huge or many global variables used in a script, I just invoke it with

```julia
julia -i A.jl

```

and then I press the three/four keys “Ctrl-D” “cursor-up” “enter” to repeat that, which takes me less than 500 ms. (But if you really need to do this regularly, you might consider putting your script into a function or module.)

---

<div class="post-metadata">

### Author: ![MetalFanatic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/metalfanatic/32/27888_2.png) [@MetalFanatic](https://discourse.julialang.org/u/MetalFanatic)
#### Post date: [January 30, 2022, 1:07am UTC](https://discourse.julialang.org/t/how-to-exit-a-julia-program-without-completely-exit-repl-or-shutting-down-julia/66655/19 "2022-01-30T01:07:30Z")

</div>

> [@CRquantum](#):
>
> 1. just wonder, in Julia, is there a command such that one can quit, but do not completely shutting down Julia?
> 
> Or is it by design that Julia only allow a complete exit?  
> However, this is also reasonable, because if it is a complete exit, all the workspace will be cleaned, and this is actually reasonable.

Thank you Mr. Biggs, that was helpful.

---

<div class="post-metadata">

### Author: ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)
#### Post date: [August 17, 2022, 8:34am UTC](https://discourse.julialang.org/t/how-to-exit-a-julia-program-without-completely-exit-repl-or-shutting-down-julia/66655/20 "2022-08-17T08:34:28Z")

</div>

> [@kristoffer.carlsson](#):
>
> At the moment no. There used to be a `workspace()` command that did this but it had some issues so it was removed. However, I think the underlying issues have been fixed so it is possible that could be put back.

Hi Kristoffer,  
are there plans to bring it back maybe renamed to e.g. to `Clear()` as proposed by Stefan K?  
The `error()` command is for me something like a workaround.  
Stefan

[Next page](https://discourse.julialang.org/t/how-to-exit-a-julia-program-without-completely-exit-repl-or-shutting-down-julia/66655.md?page=2)
