# Catch ctrl-c in script

**URL:** https://discourse.julialang.org/t/catch-ctrl-c-in-script/119885
**Category:** General Usage
**Tags:** question, interruptexception
**Created:** [September 25, 2024, 11:47pm UTC](https://discourse.julialang.org/t/catch-ctrl-c-in-script/119885 "2024-09-25T23:47:13Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![matthew3245](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@matthew3245](https://discourse.julialang.org/u/matthew3245)
#### Post date: [September 25, 2024, 11:47pm UTC](https://discourse.julialang.org/t/catch-ctrl-c-in-script/119885/1 "2024-09-25T23:47:13Z")

</div>

From the Julia docs:

> Running a Julia script using `julia file.jl` does not throw [`InterruptException`](https://docs.julialang.org/en/v1/base/base/#Core.InterruptException) when you try to terminate it with CTRL-C (SIGINT). To run a certain code before terminating a Julia script, which may or may not be caused by CTRL-C, use [`atexit`](https://docs.julialang.org/en/v1/base/base/#Base.atexit). Alternatively, you can use `julia -e 'include(popfirst!(ARGS))' file.jl` to execute a script while being able to catch `InterruptException` in the [`try`](https://docs.julialang.org/en/v1/base/base/#try) block. Note that with this strategy [`PROGRAM_FILE`](https://docs.julialang.org/en/v1/base/constants/#Base.PROGRAM_FILE) will not be set.

Here is a simple script trying to test this:

```julia
is_running = Ref(true)

t = @async while is_running[]
    println("Hello!")
    sleep(1)
end

atexit() do
    is_running[] = false
    println("Goodbye!")
    wait(t)
    exit()
end

wait(t)

```

However, when I run this script with `julia test.jl`, I get the following terminal output:

```julia
Hello!
^C
[96503] signal (2): Interrupt
in expression starting at /home/mcooper/gnc_tools/delete_this/test2.jl:15
epoll_wait at /lib/x86_64-linux-gnu/libc.so.6 (unknown line)
uv__io_poll at /workspace/srcdir/libuv/src/unix/epoll.c:236
uv_run at /workspace/srcdir/libuv/src/unix/core.c:400
ijl_task_get_next at /cache/build/builder-amdci5-1/julialang/julia-release-1-dot-10/src/partr.c:470
poptask at ./task.jl:985
wait at ./task.jl:994
#wait#645 at ./condition.jl:130
wait at ./condition.jl:125 [inlined]
_trywait at ./asyncevent.jl:142
wait at ./asyncevent.jl:159 [inlined]
sleep at ./asyncevent.jl:265 [inlined]
#105 at /home/mcooper/.julia/packages/Revise/uvGMC/src/packagedef.jl:1340
unknown function (ip: 0x78f864f01432)
_jl_invoke at /cache/build/builder-amdci5-1/julialang/julia-release-1-dot-10/src/gf.c:2894 [inlined]
ijl_apply_generic at /cache/build/builder-amdci5-1/julialang/julia-release-1-dot-10/src/gf.c:3076
jl_apply at /cache/build/builder-amdci5-1/julialang/julia-release-1-dot-10/src/julia.h:1982 [inlined]
start_task at /cache/build/builder-amdci5-1/julialang/julia-release-1-dot-10/src/task.c:1238
unknown function (ip: (nil))
Allocations: 2907 (Pool: 2899; Big: 8); GC: 0
Goodbye!

```

I can see that “Goodbye!” did get printed above, but it is there a way to stop the segmentation fault?

Cheers for the help.

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [September 26, 2024, 8:09am UTC](https://discourse.julialang.org/t/catch-ctrl-c-in-script/119885/2 "2024-09-26T08:09:56Z")

</div>

You could try calling [`Base.exit_on_sigint(false)`](https://docs.julialang.org/en/v1/base/c/#Base.exit_on_sigint) and then swallow the `InterruptException` that `wait` throws with `try catch`

---

<div class="post-metadata">

### Author: ![matthew3245](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@matthew3245](https://discourse.julialang.org/u/matthew3245)
#### Post date: [September 27, 2024, 1:11am UTC](https://discourse.julialang.org/t/catch-ctrl-c-in-script/119885/3 "2024-09-27T01:11:35Z")

</div>

Thank you that works and the program exits cleanly. However, one downside is that I need all these try and catch statements everywhere now. For example if I had two async tasks, it would look like as follows:

```julia
Base.exit_on_sigint(false)

t1 = @async while true
    try
        println("Hello")
        sleep(1)
    catch
        println("Caught an exception!")
        exit()
    end
end

t2 = @async while true
    try
        println("World!")
        sleep(1)
    catch
        println("Caught an exception!")
        exit()
    end
end

wait.([t1, t2])

```

Do you know if there is anyway to simplify this? Cheers.
