如何在julia中捕获linux信号

如何在julia中捕获linux信号,julia,Julia,我正在寻找一种在linux主机上用julia脚本捕获SIGINT的方法,但我不知道如何管理信号 在答复中: julia > try sleep(1000) catch e @info "interrupt captured!" end Ctrl-C [ Info: interrupt captured! 相反,执行demo.jl: try sleep(1000) catch e @info "interrupt capture

我正在寻找一种在linux主机上用julia脚本捕获
SIGINT
的方法,但我不知道如何管理信号

在答复中:

julia > try
    sleep(1000)
catch e
    @info "interrupt captured!"
end
Ctrl-C
[ Info: interrupt captured!
相反,执行
demo.jl

try
    sleep(1000)
catch e
    @info "interrupt captured!"
end
给出:

terminal> julia demo.jl
Ctrl-C

signal (2): Interrupt
in expression starting at /tmp/demo.jl:3
epoll_pwait at /lib/x86_64-linux-gnu/libc.so.6 (unknown line)
uv__io_poll at /workspace/srcdir/libuv/src/unix/linux-core.c:270
uv_run at /workspace/srcdir/libuv/src/unix/core.c:359
jl_task_get_next at /buildworker/worker/package_linux64/build/src/partr.c:473
poptask at ./task.jl:704
wait at ./task.jl:712 [inlined]
...

在这种情况下如何管理中断?

我不会使用异常如果您只想捕获SIGINT信号,我会设置一个处理程序。您应该看看atexit(f)函数

atexit() do
    
   //Handle your exception here if necessary

end
编辑:如果第一种方法不起作用,请尝试以下方法:

atexit(exitFunc()

      //Code to be executed when the signal is received

end)
如上所述,当您执行julia demo.jl时,Ctrl-C不会引发中断异常。改用:

Base.exit_on_sigint(false)

try
   while true
      sleep(1)
      @info "."
   end
catch e
   @info "interrupt captured!"
end

我认为sleep(1000)在这种情况下没有帮助。

就像它不工作一样:使用
atexit
处理程序,它仍然会发出未加补丁的
信号(2):中断
谢谢,它工作了。我只是不清楚为什么长睡眠超时值不能捕获异常。