# Looking for the correct way to do a clean julia process shutdown

**URL:** <https://discourse.julialang.org/t/looking-for-the-correct-way-to-do-a-clean-julia-process-shutdown/46691>\
**Category:** General Usage\
**Created:** [September 16, 2020, 8:25am UTC](https://discourse.julialang.org/t/looking-for-the-correct-way-to-do-a-clean-julia-process-shutdown/46691 "2020-09-16T08:25:11Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [May 20, 2021, 3:42pm UTC](https://discourse.julialang.org/t/looking-for-the-correct-way-to-do-a-clean-julia-process-shutdown/46691/6 "2021-05-20T15:42:26Z")

</div>

Someone asked me this question offline and directed my attention here, so I thought I would post a reply here too. This exact problem came up in Julia’s base test/runtests.jl driver script. I’ve extracted the relevant code, and modified it some for clarity. Modify this to meet your needs, if this is applicable to you.

The key pieces are:  
`raw!` – tells the OS to send the ^C keypresses synchronously instead of interrupting the currently running task with a signal  
`user_close_function` – whatever you want it to be

```julia
using REPL
...
monitor = @async begin
    term = REPL.Terminals.TTYTerminal("xterm", stdin, stdout, stderr)
    try
        REPL.Terminals.raw!(term, true)
        while !eof(term)
            c = read(term, Char)
            if c == '\x3' # ^C
                print("^C\n")
                break
            elseif c == '\x4' # ^D
                break
            else
                Base.escape_string(stdout, string(c))
            end
        end
    finally
        REPL.Terminals.raw!(term, false)
    end
    user_close_function()
end
...
wait(monitor)

```

---

_[View the full topic](https://discourse.julialang.org/t/looking-for-the-correct-way-to-do-a-clean-julia-process-shutdown/46691)._
