2017-02-19 33 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