2017-02-19 36 views
0

我想写一个escript,当它接收到HUP信号时重新加载它的配置。当我启动escript时,我在OS X上搜索活动监视器中的任何新进程。当我这样做时,会弹出:inet_gethost(两次),erl_child_setup和beam.swp。当我发送一个SIGHUP给erl_child_setup时,它会以“erl_child_setup关闭”的消息崩溃。当我发送给beam.swp时,我收到一条“Hangup:1”的消息,但我的陷阱代码没有被调用。在Elixir escript中捕获出口信号

下面是一些示例代码,说明什么,我试图做的:

defmodule TrapHup do 

    def main(args) do 
    Process.flag(:trap_exit, true) 
    main_loop() 
    end 

    def main_loop() do 
    receive do 
     { :EXIT, _from, reason } -> 
     IO.puts "Caught exit!" 
     IO.inspect reason 
     main_loop() 
    end 
    end 
end 

回答

0

我发现这是不可能只用药剂/二郎。显然,这是有可能通过bash作为在这个要点说明:https://gist.github.com/Djo/bfa9fa75928ce432ec51

下面的代码:

#!/usr/bin/env bash 
set -x 

term_handler() { 
    echo "Stopping the server process with PID $PID" 
    erl -noshell -name "[email protected]" -eval "rpc:call('[email protected]', init, stop, [])" -s init stop 
    echo "Stopped" 
} 

trap 'term_handler' TERM INT 

elixir --name [email protected] -S mix run --no-halt & 
PID=$! 

echo "Started the server process with PID $PID" 
wait $PID 

# remove the trap if the first signal received or 'mix run' stopped for some reason 
trap - TERM INT 

# return the exit status of the 'mix run' 
wait $PID 
EXIT_STATUS=$? 

exit $EXIT_STATUS 
+0

这是品质的回答非常差。链接不应该按原样发布,请阅读此资源的规则。相反的答案,解释howtos等,和相关的代码片段应该在这里发布。 – mudasobwa

+0

已用链接中的短代码片段更新。 – eltiare